# Class.new and local variables

**URL:** <https://www.ruby-forum.com/t/class-new-and-local-variables/178925>\
**Category:** Ruby\
**Created:** [November 13, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/class-new-and-local-variables/178925 "2009-11-13T16:16:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2009, 4:16pm UTC](https://www.ruby-forum.com/t/class-new-and-local-variables/178925/1 "2009-11-13T16:16:43Z")

</div>

I’m trying to do something similar to the Camping ‘R’ class generator.  
Can anyone explain to me how to get the following to print out from the  
program below:

```
 param is p and arg is this is a P
 param is q and arg is this is a Q

```

(basically I want to capture the argument to the maker functions inside  
the class they generate).

---- cut here — cut here — cut here —

## Using this function causes a blank after “arg is”

def maker1(thearg)  
Class.new do  
@arg = thearg.dup  
def self.argval  
@arg  
end  
def doit(param)  
puts “param is #{param} and arg is #{self.class.argval}”  
end  
end  
end

## Using this function causes the same value to be printed out for all

classes  
def maker2(thearg)  
Class.new do  
@@arg = thearg.dup  
def doit(param)  
puts “param is #{param} and arg is #{@@arg}”  
end  
end  
end

## using this function causes a undefined ‘local variable or method’

def maker3(thearg)  
Class.new do  
def doit(param)  
puts “param is #{param} and arg is #{thearg}”  
end  
end  
end

# change maker1 to maker2 or maker3

class P \< maker1 ‘this is a P’  
def something  
doit(‘p’)  
end  
end

# change maker1 to maker2 or maker3

class Q \< maker1 ‘this is a Q’  
def something  
doit(‘q’)  
end  
end

P.new.something  
Q.new.something

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2009, 4:47pm UTC](https://www.ruby-forum.com/t/class-new-and-local-variables/178925/2 "2009-11-13T16:47:45Z")

</div>

Hi –

On Sat, 14 Nov 2009, James Brister wrote:

> ---- cut here — cut here — cut here —  
> end  
> end  
> end

Instance variables are strictly per object. The class you get back  
from maker1 is not the same object as any of its subclasses.

> ## Using this function causes the same value to be printed out for all
> 
> classes  
> def maker2(thearg)  
> Class.new do  
> @@arg = thearg.dup  
> def doit(param)  
> puts “param is #{param} and arg is #{@@arg}”  
> end  
> end  
> end

Class variables are weird 🙂

> ## using this function causes a undefined ‘local variable or method’
> 
> def maker3(thearg)  
> Class.new do  
> def doit(param)  
> puts “param is #{param} and arg is #{thearg}”  
> end  
> end  
> end

The def keyword starts a new scope.

However… the define\_method method doesn’t:

def maker3(thearg)  
Class.new do  
define\_method(:doit) do |param|  
puts “param is #{param} and arg is #{thearg}”  
end  
end  
end

In this version, I’m using only code blocks (and no 'def’s). Code  
blocks do see the local variables that exist at the point where the  
code block starts. So thearg is in scope. Also, the block you provide  
to define\_method becomes, basically, the body of the method. I define  
it to take one argument (note: the parameter syntax and binding  
semantics for this have changed in Ruby 1.9, but they’ve actually  
changed in a way that makes blocks and methods more rather than less  
congruent), and of course that argument is in scope too.

David

–  
The Ruby training with D. Black, G. Brown, J.McAnally  
Compleat Jan 22-23, 2010, Tampa, FL  
Rubyist [http://www.thecompleatrubyist.com](http://www.thecompleatrubyist.com)

David A. Black/Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [November 13, 2009, 5:02pm UTC](https://www.ruby-forum.com/t/class-new-and-local-variables/178925/3 "2009-11-13T17:02:16Z")

</div>

Wow. I go get coffee and the solution arrives… excellent! Thanks.

James

David A. Black wrote:

[the solution to my problem]
