# Variable in lambda

**URL:** <https://www.ruby-forum.com/t/variable-in-lambda/139604>\
**Category:** Ruby\
**Created:** [June 5, 2008, 11:12am UTC](https://www.ruby-forum.com/t/variable-in-lambda/139604 "2008-06-05T11:12:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![salamond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@salamond](https://www.ruby-forum.com/u/salamond)\
**Post date:** [June 5, 2008, 11:12am UTC](https://www.ruby-forum.com/t/variable-in-lambda/139604/1 "2008-06-05T11:12:57Z")

</div>

Hi, all.

I came across the following problem.  
I need to generate a couple of methods,  
by now, I’m using  
acode = lambda {  
puts "i am a student in class " + c  
}

bcode = lambda {  
puts "class "+c+ “is a great class”  
}

[“a”, “b”, “c”].each do |c|  
define\_method(“student\_in\_class”+c, acode)  
define\_method(“greate\_class”+c, bcode)  
end

now ruby doesn’t seem to recognize c in acode and bcode.  
is there any alternatives?

thx  
JarodZZ

---

<div class="post-metadata">

**Author:** ![salamond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@salamond](https://www.ruby-forum.com/u/salamond)\
**Post date:** [June 5, 2008, 11:41am UTC](https://www.ruby-forum.com/t/variable-in-lambda/139604/2 "2008-06-05T11:41:23Z")

</div>

salamond wrote:

> Hi, all.
> 
> I came across the following problem.  
> I need to generate a couple of methods,  
> by now, I’m using  
> acode = lambda {  
> puts "i am a student in class " + c  
> }

acode = lambda { |c| lambda {  
puts "i am a student in class " + c  
}

> bcode = lambda {  
> puts "class "+c+ “is a great class”  
> }

bcode = lambda { |c| lambda {  
puts "class "+c+ “is a great class”  
}  
}

> [“a”, “b”, “c”].each do |c|  
> define\_method(“student\_in\_class”+c, acode)  
> define\_method(“greate\_class”+c, bcode)  
> end  
> [“a”, “b”, “c”].each do |c|  
> define\_method(“student\_in\_class”+c, acode.call©)  
> define\_method(“greate\_class”+c, bcode.call©)  
> end
> 
> now ruby doesn’t seem to recognize c in acode and bcode.  
> is there any alternatives?
> 
> thx  
> JarodZZ

I think this will do the trick.

onur

---

<div class="post-metadata">

**Author:** ![salamond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@salamond](https://www.ruby-forum.com/u/salamond)\
**Post date:** [June 5, 2008, 11:44am UTC](https://www.ruby-forum.com/t/variable-in-lambda/139604/3 "2008-06-05T11:44:51Z")

</div>

a missing curly brackets.

> acode = lambda { |c| lambda {  
> puts "i am a student in class " + c  
> }}

---

<div class="post-metadata">

**Author:** ![salamond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@salamond](https://www.ruby-forum.com/u/salamond)\
**Post date:** [June 5, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/variable-in-lambda/139604/4 "2008-06-05T19:46:04Z")

</div>

On 05.06.2008 11:43, Onur G. wrote:

> a missing curly brackets.
> 
> > acode = lambda { |c| lambda {  
> > puts "i am a student in class " + c  
> > }}

Much simpler

irb(main):001:0\> class F  
irb(main):002:1\> %w{a b c}.each {|c| define\_method© { c }}  
irb(main):003:1\> end  
=\> [“a”, “b”, “c”]  
irb(main):004:0\> F.new.a  
=\> “a”  
irb(main):005:0\> F.new.b  
=\> “b”  
irb(main):006:0\> F.new.c  
=\> “c”  
irb(main):007:0\>

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![salamond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@salamond](https://www.ruby-forum.com/u/salamond)\
**Post date:** [June 6, 2008, 8:35am UTC](https://www.ruby-forum.com/t/variable-in-lambda/139604/5 "2008-06-06T08:35:55Z")

</div>

On 06.06.2008 03:40, salamond wrote:

> most of my unit test cases are like something above, and I want to  
> express all cases in data,  
> automatically generate cases in the run time
> 
> Do u think it’s possible?

A lot is possible. But from what you write it is not clear what “it” in  
this case is. Can you be more concrete?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![salamond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@salamond](https://www.ruby-forum.com/u/salamond)\
**Post date:** [June 6, 2008, 3:42am UTC](https://www.ruby-forum.com/t/variable-in-lambda/139604/6 "2008-06-06T03:42:16Z")

</div>

many thanks for the help, Robert and Onur.

I tried Onur’s way, it works.  
But I don’t quite get the later simpler method, I’ll try it later.

I’m using this method in UnitTest.  
Have you guys wondered, why Ruby Unit Test can’t do something like this:

class Test\_${name} {  
def setup {}  
def test\_${step1} {}  
def test\_${step2} {}  
def teardown {}  
}

most of my unit test cases are like something above, and I want to  
express all cases in data,  
automatically generate cases in the run time

Do u think it’s possible?

JarodZZ~

On Fri, Jun 6, 2008 at 1:44 AM, Robert K.

---

<div class="post-metadata">

**Author:** ![salamond](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@salamond](https://www.ruby-forum.com/u/salamond)\
**Post date:** [June 7, 2008, 9:25pm UTC](https://www.ruby-forum.com/t/variable-in-lambda/139604/7 "2008-06-07T21:25:06Z")

</div>

sure, check below,  
see I have cases like:

TC1 = { name=\>“touch”, setup =\> "mkdir1 ", test\_step1=\>“touch file”,  
test\_step2=\>“cat file”, teardown =\> “rm dir 1”}  
TC2 = { name=\>“echo” setup =\> “mkdir 2”, test\_step1=\>“echo 1 \> file”,  
test\_step2=\>“cat file”, teardown =\> “rm dir 2”}

Can I use a case\_reader or case\_generator to generate 2 testcases for  
the above data?

thx  
JarodZZ

On Fri, Jun 6, 2008 at 2:33 PM, Robert K.
