# Getting an Error with local variable

**URL:** <https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117>\
**Category:** Ruby\
**Created:** [March 30, 2011, 4:28am UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117 "2011-03-30T04:28:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 30, 2011, 4:28am UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117/1 "2011-03-30T04:28:37Z")

</div>

class String  
remove\_method(:each)  
end

class Object  
def reach  
if(x.respond\_to?(:each))  
self.each{|x| x.reach(yield)}  
else  
yield(self)  
end  
end  
end

This is my code… I keep getting the error that it can’t find the local  
variable ‘x’ … why???

---

<div class="post-metadata">

**Author:** ![cheryl\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@cheryl\_m](https://www.ruby-forum.com/u/cheryl_m)\
**Post date:** [March 30, 2011, 4:44am UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117/2 "2011-03-30T04:44:34Z")

</div>

Hi, Cheryl

> if(x.respond\_to?(:each))  
> x is not defined here.  
> This is not x but self, isn’t it?

The following code works well.

class String  
remove\_method(:each)  
end

class Object  
def reach  
if(self.respond\_to?(:each)) # \<\<\<  
self.each{|x| x.reach(yield)}  
else  
yield(self)  
end  
end  
end

“hoge”.reach { |myself|  
print myself  
}

---

<div class="post-metadata">

**Author:** ![cheryl\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@cheryl\_m](https://www.ruby-forum.com/u/cheryl_m)\
**Post date:** [March 30, 2011, 4:45am UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117/3 "2011-03-30T04:45:47Z")

</div>

Because you use x here:

if(x.respond\_to?(:each))

But it hasn’t been defined.

You probably just want to remove that x.

---

<div class="post-metadata">

**Author:** ![cheryl\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@cheryl\_m](https://www.ruby-forum.com/u/cheryl_m)\
**Post date:** [March 30, 2011, 4:48am UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117/4 "2011-03-30T04:48:29Z")

</div>

now im getting the error:

multiple values for the block paramater(0 for 1)

---

<div class="post-metadata">

**Author:** ![cheryl\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@cheryl\_m](https://www.ruby-forum.com/u/cheryl_m)\
**Post date:** [March 30, 2011, 5:04am UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117/5 "2011-03-30T05:04:55Z")

</div>

new error:: reach stack level too deep

---

<div class="post-metadata">

**Author:** ![cheryl\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@cheryl\_m](https://www.ruby-forum.com/u/cheryl_m)\
**Post date:** [March 30, 2011, 5:01am UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117/6 "2011-03-30T05:01:10Z")

</div>

> multiple values for the block paramater(0 for 1)

You cannot set an argument for x.reach.  
self.each{|x| x.reach(yield)}

Instead,  
self.each{|x| x.reach{|y| yield y}}  
This works but is not elegant.  
Somebody has an idea?

---

<div class="post-metadata">

**Author:** ![cheryl\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@cheryl\_m](https://www.ruby-forum.com/u/cheryl_m)\
**Post date:** [March 30, 2011, 2:19pm UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117/7 "2011-03-30T14:19:51Z")

</div>

Robert, thank you for the reply.

On Wed, Mar 30, 2011 at 7:59 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > self.each{|x| x.reach{|y| yield y}}  
> > This works but is not elegant.  
> > Somebody has an idea?
> 
> The approach is broken because the block is not carried through the  
> recursion.

I read your previous post. I like it.  
It is elegant to useblock instead of yield and to add reach method to  
Enumerable module.

I am curious about when my code does not work.  
I tested the next script and it printed the same as your code.

class String  
remove\_method(:each)  
end

class Object  
def reach  
if(self.respond\_to?(:each))  
self.each{|x| x.reach{|y| yield y}}  
else  
yield(self)  
end  
end  
end

[[“hoge”, “huga”],[“foo”, “bar”]].reach { |myself|  
print myself  
}

With a nested array, a new block is given to the next ‘reach’.  
So, I think, if I write this without recursion (of course impossible),  
the whole prosess is like  
some\_array.each{|x1| x1.each{|x2| x2.each{ … xn.each {|myself|  
print myself} …}}}  
and xn is an unnested array.

If I misunderstand something, or my program has a bug, please let me  
know.

Thanks.

Haruka YAGNI

---

<div class="post-metadata">

**Author:** ![cheryl\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@cheryl\_m](https://www.ruby-forum.com/u/cheryl_m)\
**Post date:** [March 30, 2011, 1:01pm UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117/8 "2011-03-30T13:01:21Z")

</div>

On Wed, Mar 30, 2011 at 5:00 AM, Haruka YAGNI [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > multiple values for the block paramater(0 for 1)
> 
> You cannot set an argument for x.reach.  
> self.each{|x| x.reach(yield)}
> 
> Instead,  
> self.each{|x| x.reach{|y| yield y}}  
> This works but is not elegant.  
> Somebody has an idea?

The approach is broken because the block is not carried through the  
recursion. Please see my recent reply in thread “Creating a Reach  
Program” a few days back. Cheryl, did you actually read it?

robert

---

<div class="post-metadata">

**Author:** ![cheryl\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@cheryl\_m](https://www.ruby-forum.com/u/cheryl_m)\
**Post date:** [March 30, 2011, 2:37pm UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117/9 "2011-03-30T14:37:12Z")

</div>

On Wed, Mar 30, 2011 at 2:14 PM, Haruka YAGNI [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I read your previous post. I like it.  
> It is elegant to useblock instead of yield and to add reach method to  
> Enumerable module.
> 
> I am curious about when my code does not work.  
> I tested the next script and it printed the same as your code.
> 
> class String  
> remove\_method(:each)  
> end

This is only needed in 1.8.\* btw.

# $ allruby -e ‘String.instance\_method “each”’ CYGWIN\_NT-5.1 padrklemme2 1.7.9(0.237/5/3) 2011-03-29 10:10 i686 Cygwin

# ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]

# ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-cygwin] -e:1:in `instance_method': undefined method `each’ for class `String' (NameError) from -e:1:in `’

# jruby 1.6.0 (ruby 1.8.7 patchlevel 330) (2011-03-15 f3b6154) (Java HotSpot™ Client VM 1.6.0\_24) [Windows XP-x86-java]

jruby 1.6.0 (ruby 1.9.2 patchlevel 136) (2011-03-15 f3b6154) (Java  
HotSpot™ Client VM 1.6.0\_24) [Windows XP-x86-java]  
NameError: undefined method `each' for class `String’  
instance\_method at org/jruby/RubyModule.java:1957  
(root) at -e:1

> [[“hoge”, “huga”],[“foo”, “bar”]].reach { |myself|  
> If I misunderstand something, or my program has a bug, please let me know.  
> I’m afraid the bug is in my brain. You are right. I am sorry. I  
> should have taken more time to look at this.

That approach does produce awful call stacks though (with multiple  
levels of nesting the call chain has to go backwards up to the topmost  
caller to invoke the block initially provided to reach; you can see  
this by adding “puts caller” to the beginning of the block). I find it  
more elegant to pass the block down and have it invoked immediately.  
Apparently you agree with me here.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![cheryl\_m](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@cheryl\_m](https://www.ruby-forum.com/u/cheryl_m)\
**Post date:** [March 30, 2011, 2:50pm UTC](https://www.ruby-forum.com/t/getting-an-error-with-local-variable/205117/10 "2011-03-30T14:50:11Z")

</div>

On Wed, Mar 30, 2011 at 9:36 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > recursion.  
> > > end
> 
> I’m afraid the bug is in my brain. You are right. I am sorry. I  
> should have taken more time to look at this.

No problem at all.

> That approach does produce awful call stacks though (with multiple  
> levels of nesting the call chain has to go backwards up to the topmost  
> caller to invoke the block initially provided to reach; you can see  
> this by adding “puts caller” to the beginning of the block). I find it  
> more elegant to pass the block down and have it invoked immediately.  
> Apparently you agree with me here.

Yes, I agree at all points.  
When I do something like this in my application, I will use your  
solution with appreciation.

Thank you for you kind information.

regards

Haruka YAGNI
