Variable in lambda

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

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

a missing curly brackets.

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

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

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

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.

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.