Version issues

D

David Vlad

I am using ruby 1.8.6 and I have been having some problem using certain
methods and stuff. It seems that my ruby wont recognize until loops and
also the .collect method dosnt work.

Is this normal? If I try to write use a until loop in my code I get
error and if I try to use .collect nothing happens (but no error).
 
L

Luis Lavena

I am using ruby 1.8.6 and I have been having some problem using certain
methods and stuff. It seems that my ruby wont recognize until loops and
also the .collect method dosnt work.

Is this normal? If I try to write use a until loop in my code I get
error and if I try to use .collect nothing happens (but no error).

Please show a code example, the output you're getting and the exact
Ruby version you're using.

Help us help you
 
B

brabuhr

I am using ruby 1.8.6 and I have been having some problem using certain
methods and stuff. It seems that my ruby wont recognize until loops and
also the .collect method dosnt work.

Is this normal? If I try to write use a until loop in my code I get
error and if I try to use .collect nothing happens (but no error).

Please provide a short example of the code you are trying and the
incorrect output you are receiving.

Thanks.

PS
( http://catb.org/~esr/faqs/smart-questions.html )
 
R

Ryan Davis

I am using ruby 1.8.6 and I have been having some problem using
certain
methods and stuff. It seems that my ruby wont recognize until loops
and
also the .collect method dosnt work.

Is this normal? If I try to write use a until loop in my code I get
error and if I try to use .collect nothing happens (but no error).

Is what normal? We can't help you if we can't see what you're trying
to do. "doesn't work" is not descriptive.
 
D

David Vlad

I apologize. I actually managed to get the until loops working after
some reinstalling. However, the .collect method still dosnt work and I
really want to use it. The code looks like this:


array = [1,2,5,7]

array.collect do |x|
x = x*2
end

puts array.inspect


After I have run this code it still says the array is [1,2,5,7].
 
R

Ryan Davis

I apologize. I actually managed to get the until loops working after
some reinstalling. However, the .collect method still dosnt work and I
really want to use it. The code looks like this:


array = [1,2,5,7]

array.collect do |x|
x = x*2
end

puts array.inspect


After I have run this code it still says the array is [1,2,5,7].

% ri Array.collect
---------------------------------------------------------- Array#collect
array.collect {|item| block } -> an_array
array.map {|item| block } -> an_array
------------------------------------------------------------------------
Invokes _block_ once for each element of _self_. Creates a new
array containing the values returned by the block. See also
+Enumerable#collect+.

a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
a #=> ["a", "b", "c", "d"]
 
D

David Vlad

Ryan said:
---------------------------------------------------------- Array#collect
array.collect {|item| block } -> an_array
array.map {|item| block } -> an_array
------------------------------------------------------------------------
Invokes _block_ once for each element of _self_. Creates a new
array containing the values returned by the block. See also
+Enumerable#collect+.

a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
a #=> ["a", "b", "c", "d"]

I know what it does, but still it dosnt work.
 
W

William Rutiser

David said:
I apologize. I actually managed to get the until loops working after
some reinstalling. However, the .collect method still dosnt work and I
really want to use it. The code looks like this:


array = [1,2,5,7]

array.collect do |x|
x = x*2
end

puts array.inspect


After I have run this code it still says the array is [1,2,5,7].

"array.collect " isn't supposed to modify array.

"array.collect" returns a newly constructed array containing the block's
results.

Compare

irb(main):001:0> array = [1,2,5,7]
=> [1, 2, 5, 7]
irb(main):002:0>
irb(main):003:0* array.collect do |x|
irb(main):004:1* x = x*2
irb(main):005:1> end
=> [2, 4, 10, 14]
irb(main):006:0>
irb(main):007:0* puts array.inspect
[1, 2, 5, 7]
=> nil

with

irb(main):008:0> array = [1,2,5,7]
=> [1, 2, 5, 7]
irb(main):009:0> puts array.collect do |x|
irb(main):010:1* x = x*2
irb(main):011:1> end
1
2
5
7
=> nil
irb(main):012:0> puts array.inspect
[1, 2, 5, 7]
=> nil
 
S

Sebastian Hungerecker

I know what it does, but still it dosnt work.


The behavior of the code you posted is entirely consistent with
the described behavior and the example in the docs which Ryan posted
(in the example the contents of a are the same before and after calling
collect on it). So if collect does not work for you, show an example in
which it doesn't work (as opposed to one where it behaves exactly as
expected).

To summarize the important part of the docs: collect returns a *new* array,
it does not change the array it is called on in any way.
 
D

David Vlad

Im so sorry, I have misunderstood everything. I thought it was supposed
to modify the array not create a new one within the block.

Is there any method that actually does modify the array or will I have
to do something like this:

array = [1,3,7,8]
i = 0

while i < array.length
array = array * 2
i += 1
end
 
R

Ryan Davis

Ryan said:
---------------------------------------------------------- Array#collect
array.collect {|item| block } -> an_array
array.map {|item| block } -> an_array
------------------------------------------------------------------------
Invokes _block_ once for each element of _self_. Creates a new
array containing the values returned by the block. See also
+Enumerable#collect+.

a = [ "a", "b", "c", "d" ]
a.collect {|x| x + "!" } #=> ["a!", "b!", "c!", "d!"]
a #=> ["a", "b", "c", "d"]

I know what it does, but still it dosnt work.

You seem to be missing the big clue: "CREATES A NEW ARRAY"

P.S. "it doesn't work" is NOT descriptive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top