variable in lambda

S

salamond

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
 
O

Onur Gungor

salamond said:
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(c))
define_method("greate_class"+c, bcode.call(c))
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
 
R

Robert Klemme

a missing curly brackets.

Much simpler

irb(main):001:0> class F
irb(main):002:1> %w{a b c}.each {|c| define_method(c) { 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
 
S

salamond

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~

a missing curly brackets.

Much simpler

irb(main):001:0> class F
irb(main):002:1> %w{a b c}.each {|c| define_method(c) { 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
 
R

Robert Klemme

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?

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
 
S

salamond

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top