# Local variable and closure

**URL:** <https://www.ruby-forum.com/t/local-variable-and-closure/99943>\
**Category:** Ruby\
**Created:** [February 1, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/local-variable-and-closure/99943 "2006-02-01T23:09:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [February 1, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/local-variable-and-closure/99943/1 "2006-02-01T23:09:49Z")

</div>

Hello,

below is one little script that fails at ### \<\<\< line  
the output is

Animal has properties  
Animal will be speakable  
Dog has properties  
Dog will be speakable  
Duck has properties  
Duck will be speakable  
…/dort.rb:16:in `speak': undefined local variable or method`msg’ for  
#Dog:0x401d8da0 (NameError)  
from ./dort.rb:44

I would think that (passed to function) local variable msg would be  
“closed” in self.class\_eval {} closure and therefor be accessable

can someone point out why it doesn’t work?

Regards, Daniel

#!/usr/bin/env ruby

$prop = {}

def prop  
puts “#{self.name} has properties”  
self.class\_eval do  
$prop[self] = []  
end  
end

def speakable(msg)  
puts “#{self.name} will be speakable”  
self.class\_eval do  
$prop[self] \<\< “I can speak”  
def speak;msg;end ### \<\<\<  
end  
end

class Animal  
prop  
speakable “…”  
def whatCanIDo;$prop[self.class];end  
def self.whatCanIDo;$prop[self];end  
end

class Dog \< Animal  
prop  
speakable “wuff”  
end

class Duck \< Animal  
prop  
speakable “quak”  
end

a = Dog.new  
b = Duck.new

puts a.speak, b.speak

---

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [February 1, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/local-variable-and-closure/99943/2 "2006-02-01T23:09:49Z")

</div>

Schüle Daniel wrote:

> Duck will be speakable
> 
> end  
> prop  
> class Duck \< Animal  
> prop  
> speakable “quak”  
> end
> 
> a = Dog.new  
> b = Duck.new
> 
> puts a.speak, b.speak

Without looking too close at your code: def has special scoping rules.  
You better try to use define\_method:

def speakable(msg)  
class\_eval do  
# …  
define\_method(:speak) msg end  
end  
end

HTH

Btw, what are you trying to accomplish? Somehow I have the feeling that  
your code might be more complex than needed. Especially the use of a  
global variable irritates me.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [February 1, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/local-variable-and-closure/99943/3 "2006-02-01T23:12:51Z")

</div>

Hello Robert,

> Btw, what are you trying to accomplish? Somehow I have the feeling that  
> your code might be more complex than needed. Especially the use of a  
> global variable irritates me.

complexity was the intention of my code 🙂  
no really, I am playing with metaprogramming, also I wanted to know how  
to implement own functions like “attr\_accessor”  
btw is there special name for this kind of functions?  
functions that are executed in the class scope and can refer with “self”  
to the class they are inside.

Regards, Daniel

---

<div class="post-metadata">

**Author:** ![SchSSS\_le\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a183cd/32.png) [@SchSSS\_le\_Daniel](https://www.ruby-forum.com/u/SchSSS_le_Daniel)\
**Post date:** [February 1, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/local-variable-and-closure/99943/4 "2006-02-01T23:19:01Z")

</div>

Schüle Daniel wrote:

> Hello Robert,
> 
> > Btw, what are you trying to accomplish? Somehow I have the feeling  
> > that your code might be more complex than needed. Especially the  
> > use of a global variable irritates me.
> 
> complexity was the intention of my code 🙂

🙂

> no really, I am playing with metaprogramming, also I wanted to know  
> how to implement own functions like “attr\_accessor”

One way:

class Module  
def i\_am\_in\_class\_scope(\*a)  
p a  
end  
end

> > class Foo  
> > i\_am\_in\_class\_scope :foo, :bar  
> > end  
> > [:foo, :bar]  
> > =\> nil

> btw is there special name for this kind of functions?  
> functions that are executed in the class scope and can refer with  
> “self” to the class they are inside.

I’d call them “class instance methods”.

Kind regards

```
robert
```
