# Each, stuck up

**URL:** <https://www.ruby-forum.com/t/each-stuck-up/146448>\
**Category:** Ruby\
**Created:** [August 21, 2008, 9:14am UTC](https://www.ruby-forum.com/t/each-stuck-up/146448 "2008-08-21T09:14:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [August 21, 2008, 9:14am UTC](https://www.ruby-forum.com/t/each-stuck-up/146448/1 "2008-08-21T09:14:07Z")

</div>

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:in`signal\_handle’  
from /usr/lib/ruby/1.9.0/irb.rb:66:in `block in start' from (irb):2:in`block (4 levels) in irb\_binding’  
from (irb):2:in `each' from (irb):2 from /usr/lib/ruby/1.9.0/irb.rb:150:in`block (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:in`block 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:in`block in start’  
from /usr/lib/ruby/1.9.0/irb.rb:69:in `catch' from /usr/lib/ruby/1.9.0/irb.rb:69:in`start’  
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

---

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [August 21, 2008, 9:24am UTC](https://www.ruby-forum.com/t/each-stuck-up/146448/2 "2008-08-21T09:24:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mayuresh\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/839c29/32.png) [@Mayuresh\_K](https://www.ruby-forum.com/u/Mayuresh_K)\
**Post date:** [August 21, 2008, 9:30am UTC](https://www.ruby-forum.com/t/each-stuck-up/146448/3 "2008-08-21T09:30:30Z")

</div>

On Thu, Aug 21, 2008 at 9:10 AM, Mayuresh K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 😉  
> See this  
> ruby -e ‘a=[1];a.each{|x| a[x] = x.succ; p a}’  
> HTH  
> Robert  
> –  
> [http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

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