[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Metaprogammatically define methods.

Max Russell

11/17/2006 3:43:00 PM

I'm writing a harness for some tests that use watir. Rather than having
to boilerplate code in the definition of each test like
def test_1
load test1
end

I want to read the testnames in from a file which will be easier for the
user. What I'd like to do is something along this structure (which I
adapted from a really good blog in metaprogramming
http://invisibleblocks.wordpress.com/2006/06/14/ruby-meta-programming-...):

testlist = Array.new

File.open('readme.txt', 'r') do |eachline|
while (line = eachline.gets)
testlist << line
end
end
#puts testlist

testlist.each { |entry|
code = <<METHOD_TEMPLATE
def test_#{entry}()
puts "testing #{entry}"
end
METHOD_TEMPLATE
}


and then call each method I've defined. However, I'm not sure I'm going
about this the correct way.

--
Posted via http://www.ruby-....

8 Answers

dblack

11/17/2006 4:58:00 PM

0

Suraj Kurapati

11/18/2006 7:53:00 AM

0

Max Russell wrote:
> testlist = Array.new
>
> File.open('readme.txt', 'r') do |eachline|
> while (line = eachline.gets)
> testlist << line
> end
> end
> #puts testlist

testlist = File.open('readme.txt').readlines

>
> testlist.each { |entry|
> code = <<METHOD_TEMPLATE
> def test_#{entry}()
> puts "testing #{entry}"
> end
> METHOD_TEMPLATE
> }
> and then call each method I've defined. However, I'm not sure I'm going
> about this the correct way.

Use define_method whenever possible. The only case where you would put
code in a string (and then load it using eval) is if your methods need
to accept a block argument (define_method does not accept block argument
until Ruby 1.9).

class Foo
File.open('readme.txt').readlines.each do |entry|
define_method "test_#{entry}".to_sym do
puts "testing #{entry}"
end
end
end

Foo.new.test_whatever

--
Posted via http://www.ruby-....

Suraj Kurapati

11/18/2006 7:57:00 AM

0

Suraj Kurapati wrote:
> class Foo
> File.open('readme.txt').readlines.each do |entry|

Well it turns out that the above line can be simplified to:

File.open('readme.txt').each_line do |entry|

or even to:

IO.foreach('readme.txt') do |entry|

Personally, I prefer each_line because IO.foreach sounds like "for each
IO object in ..." instead of "for each line in ...".

> define_method "test_#{entry}".to_sym do
> puts "testing #{entry}"
> end
> end
> end
>
> Foo.new.test_whatever

--
Posted via http://www.ruby-....

dblack

11/18/2006 12:09:00 PM

0

Max Russell

11/20/2006 10:42:00 AM

0

> Use define_method whenever possible. The only case where you would put
> code in a string (and then load it using eval) is if your methods need
> to accept a block argument (define_method does not accept block argument
> until Ruby 1.9).
>
> class Foo
> File.open('readme.txt').readlines.each do |entry|
> define_method "test_#{entry}".to_sym do
> puts "testing #{entry}"
> end
> end
> end
>
> Foo.new.test_whatever

I still run into undefined method errors using this format. Are there
any restrictions on the format the generated method name can take?

--
Posted via http://www.ruby-....

Max Russell

11/24/2006 10:00:00 AM

0

Suraj Kurapati wrote:
> Suraj Kurapati wrote:
>> class Foo
>> File.open('readme.txt').readlines.each do |entry|
>
> Well it turns out that the above line can be simplified to:
>
> File.open('readme.txt').each_line do |entry|
>
> or even to:
>
> IO.foreach('readme.txt') do |entry|
>
> Personally, I prefer each_line because IO.foreach sounds like "for each
> IO object in ..." instead of "for each line in ...".
>
>> define_method "test_#{entry}".to_sym do
>> puts "testing #{entry}"
>> end
>> end
>> end
>>
>> Foo.new.test_whatever

Got a bit of help on this one and the solution I'm now going with is:

File.open('readme.txt').each_line do |entry|
self.send(:define_method, entry.strip.to_sym){ puts "testing
#{entry}" }
#self.send(:public, entry.strip.to_sym )
end
end


--
Posted via http://www.ruby-....

dblack

11/24/2006 12:54:00 PM

0

John Doe

9/27/2010 9:54:00 AM

0

On 27 Sep 2010 02:07:36 GMT, rfischer@sonic.net (Ray Fischer) wrote:

>NoBody <nobody@nowhere.com> wrote:
>> rfischer@sonic.net (Ray Fischer) wrote:
>>>NoBody <nobody@nowhere.com> wrote:
>>>> rfischer@sonic.net (Ray Fischer) wrote:
>>>>>NoBody <nobody@nowhere.com> wrote:
>
>>>>>>You cannot shove your agenda down someone else's throat if the
>>>>>>opposition can object.
>>>>>
>>>>>It's called "democracy", rightard.
>>>>
>>>>How odd that you rant if the Republicans try to stop something (as
>>>>you've done here).. That should also be "democracy".
>>>
>>>A minority imposing their will on the majority is not democracy,
>>>rightard. Clearly you're unfamiliar with the whole concept.
>>
>>Neither is the majority ramming its agenda without any input from the
>>minority.
>
>Duh yes, that is exactly what a democracy is. MAJORITY RULES.

Not if it wants to retain its majority in a democracy. A smart
majority actually works with the opposition instead of running it
over. The results of the upcoming election will prove that theory.

>
>> Of course you've blamed the Republicans for nothing getting
>>done when they are the minority.
>
>Because they block everything they can, even though they are the
>minority.

Duh yes, that is exactly how our process works and the Republicans
have done their job. Of course, you will be among the first to
declare, when the Dems are in the minority again, that when *they*
fillibuster, they are doing their "constitutional duty" by doing so.


>
>> Even if that were true, wouldn't
>>that also be "democracy"?
>
>Duh, no it wouldn't, because that's not majority rules.

We don't have a true democracy in this country - it's more of a
representational Republic.

>
>You _really_ are not clear on the concept.
>

Irony anyone?

>>>> I don't expect
>>>>that the minority gets to make the rules.
>>>
>>>Just above you're saying that they should.
>>
>>Quote where I said this.
>
>Above, where you claim a majority vote is not democracy if it is
>"the majority ramming its agenda without any input from the minority".

This does not say "the minority gets to make the rules". I suggest a
course in reading comprehension for you before you look sillier than
you already do.