[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Idiom -- is there a better way?

Mark Probert

10/20/2004 4:53:00 PM


Hi ..

I am doing indirect dispatch, something like:

class Foo
def bar(fn)
eval "self.#{fn}"
end
end

class Bar < Foo
def blah
puts "blah here"
end
end

f = Bar.new
f.bar("blah")

It works however I am sure there must be a nicer way of doing the bar(fn)
stuff. Any ideas?

TIA,
-mark.
7 Answers

Kevin Howe

10/20/2004 4:59:00 PM

0

> class Foo
> def bar(fn)
> eval "self.#{fn}"
> end
> end
>
> ...nicer way of doing the bar(fn) stuff. Any ideas?
>

Replace eval "self.#{fn}" with method(fn).call


Alexey Verkhovsky

10/20/2004 5:01:00 PM

0

On Wed, 2004-10-20 at 19:54, Mark Probert wrote:
> Hi ..
>
> I am doing indirect dispatch, something like:
> class Foo
> def bar(fn)
> eval "self.#{fn}"
> end
> end

Try self.send(fn) instead.



T. Onoma

10/20/2004 5:02:00 PM

0

On Wednesday 20 October 2004 12:54 pm, Mark Probert wrote:
| Hi ..
|
| I am doing indirect dispatch, something like:
|
| class Foo
| def bar(fn)
self.send(fn)
| end
| end
|
| class Bar < Foo
| def blah
| puts "blah here"
| end
| end
|
| f = Bar.new
| f.bar("blah")
|
| It works however I am sure there must be a nicer way of doing the bar(fn)
| stuff. Any ideas?
|
| TIA,
| -mark.

--
( o _ ���
// trans.
/ \ transami@runbox.com

I don't give a damn for a man that can only spell a word one way.
-Mark Twain



Gavin Kistner

10/21/2004 1:46:00 AM

0

On Oct 20, 2004, at 10:54 AM, Mark Probert wrote:
> class Foo
> def bar(fn)
> eval "self.#{fn}"
> end
> end
>
> class Bar < Foo
> def blah
> puts "blah here"
> end
> end
>
> f = Bar.new
> f.bar("blah")

Does the function need to be a string argument?

f.bar( f.method( :blah ) ) # Pass the actual method reference

or, as others have noted:

class Foo
def bar( meth_name )
self.method(m).call
end
end
...
f.bar( :blah )



Robert Klemme

10/21/2004 7:51:00 AM

0


"Mark Probert" <probertm@nospam-acm.org> schrieb im Newsbeitrag
news:Xns9588647C9B7C4probertmnospamacmorg@198.161.157.145...
>
> Hi ..
>
> I am doing indirect dispatch, something like:
>
> class Foo
> def bar(fn)
> eval "self.#{fn}"
> end
> end
>
> class Bar < Foo
> def blah
> puts "blah here"
> end
> end
>
> f = Bar.new
> f.bar("blah")
>
> It works however I am sure there must be a nicer way of doing the
bar(fn)
> stuff. Any ideas?
>
> TIA,
> -mark.

There's no need for method bar because that is built into Ruby alread:
Object#send does the job:

class Bar
def blah
puts "blah here"
end
end


>> Bar.new.send :blah
blah here
=> nil
>> Bar.new.send "blah"
blah here
=> nil
>>

Kind regards

robert

Mark Probert

10/21/2004 5:00:00 PM

0

Hi ..

Gavin Kistner <gavin@refinery.com> wrote:

> On Oct 20, 2004, at 10:54 AM, Mark Probert wrote:
>> class Foo
>> def bar(fn)
>> eval "self.#{fn}"
>> end
>> end
>
> Does the function need to be a string argument?
>

Yup. What I am doing is taking a YAML entry and converting it into a
method call for a derived class.

Thanks to all for the help.


--
-mark. (probertm @ acm dot org)

John Carter

10/21/2004 9:00:00 PM

0