Version issues

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).

On Fri, Mar 19, 2010 at 6:11 PM, David V.
[email protected] wrote:

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
( How To Ask Questions The Smart Way )

On Mar 19, 2010, at 3:11 PM, David V. [email protected]
wrote:

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.

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].

Ryan D. wrote:

---------------------------------------------------------- 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.

On Mar 20, 2010, at 03:51 , David V. wrote:

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"]

On Mar 19, 11:11 pm, David V. [email protected] wrote:

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

David V. wrote:

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 = x2
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

On 20.03.2010 13:25, David V. wrote:

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.

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[i] = array[i] * 2
i += 1
end

“David V.” [email protected] wrote in message
news:[email protected]

array[i] = array[i] * 2
i += 1
end

Posted via http://www.ruby-forum.com/.

use: array.collect! { … } or array.map! { … }
array = [1,3,7,8]
array.collect! { |x| x*2 } # => [2,6,14,16]
hth gfb

On 20.03.2010 17:20, GianFranco B. wrote:

array = [1,3,7,8]
array.collect! {|x| x*2 } # => [2,6,14,16]

And most importantly:
array # => [2,6,14,16]

On Mar 20, 2010, at 05:25 , David V. wrote:

   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.