Each, stuck up

Hello,

A while back there was this discussion about what code could crash irb

I did the following in irb 1.9;
a = [3, 2, 1, 0, -1, -2, -3]
a.each {|x| a[x] = x +1}

irb just got stuck, it wouldn’t do anything for a while until I hit
Ctrl+c, doing so threw up the following messages;
IRB::Abort: abort then interrupt!!
from /usr/lib/ruby/1.9.0/irb.rb:81:in irb_abort' from /usr/lib/ruby/1.9.0/irb.rb:243:insignal_handle’
from /usr/lib/ruby/1.9.0/irb.rb:66:in block in start' from (irb):2:inblock (4 levels) in irb_binding’
from (irb):2:in each' from (irb):2 from /usr/lib/ruby/1.9.0/irb.rb:150:inblock (2 levels) in
eval_input’
from /usr/lib/ruby/1.9.0/irb.rb:259:in signal_status' from /usr/lib/ruby/1.9.0/irb.rb:147:inblock in eval_input’
from /usr/lib/ruby/1.9.0/irb.rb:146:in eval_input' from /usr/lib/ruby/1.9.0/irb.rb:70:inblock in start’
from /usr/lib/ruby/1.9.0/irb.rb:69:in catch' from /usr/lib/ruby/1.9.0/irb.rb:69:instart’
from /usr/bin/irb1.9:13:in `’

After that it wouldn’t accept any more commands till I exit the
session by executing “exit”.

I’m kind-a obtuse at the moment, could someone please tell me what was
it that I did wrong.
I wanted to increment the values of each element of the array.

~Mayuresh

I did the following in irb 1.9;
a = [3, 2, 1, 0, -1, -2, -3]
a.each {|x| a[x] = x +1}

I’m kind-a obtuse at the moment, could someone please tell me what was
it that I did wrong.
I wanted to increment the values of each element of the array.

In the above example, x is the value of each member of the array, not
its
position. You want this instead:

a.map! { |x| x + 1 }

map! takes each member of an array and performs some transformation on
it,
writing the result back to the array.

On Thu, Aug 21, 2008 at 9:10 AM, Mayuresh K.
[email protected] wrote:

Hello,

A while back there was this discussion about what code could crash irb

I did the following in irb 1.9;
a = [3, 2, 1, 0, -1, -2, -3]
a.each {|x| a[x] = x +1}
looks like an endless loop to me, well and to irb too :wink:
See this
ruby -e ‘a=[1];a.each{|x| a[x] = x.succ; p a}’
HTH
Robert

http://ruby-smalltalk.blogspot.com/

There’s no one thing that’s true. It’s all true.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs