Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Idiom -- is there a better way?

Reply
Thread Tools

Idiom -- is there a better way?

 
 
Mark Probert
Guest
Posts: n/a
 
      10-20-2004

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.
 
Reply With Quote
 
 
 
 
Kevin Howe
Guest
Posts: n/a
 
      10-20-2004
> 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


 
Reply With Quote
 
 
 
 
Alexey Verkhovsky
Guest
Posts: n/a
 
      10-20-2004
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.



 
Reply With Quote
 
trans. (T. Onoma)
Guest
Posts: n/a
 
      10-20-2004
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.
/ \

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



 
Reply With Quote
 
Gavin Kistner
Guest
Posts: n/a
 
      10-21-2004
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 )



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-21-2004

"Mark Probert" <> 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

 
Reply With Quote
 
Mark Probert
Guest
Posts: n/a
 
      10-21-2004
Hi ..

Gavin Kistner <> 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)

 
Reply With Quote
 
John Carter
Guest
Posts: n/a
 
      10-21-2004
#On Thu, 21 Oct 2004, Mark Probert wrote:
#
#Try

class Foo
def bar(fn)
send(fn)
end
end

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

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




#
#John Carter Phone : (64)(3) 358 6639
#Tait Electronics Fax : (64)(3) 359 4632
#PO Box 1645 Christchurch Email :
#New Zealand
#
# ruby -wc certifies this email "Syntax OK"



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
in place list modification necessary? What's a better idiom? MooMaster Python 11 04-09-2009 05:09 AM
better idiom for title on an optional list? slothbear Ruby 3 12-04-2007 09:27 PM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM
Is there such an idiom? Per Python 8 03-20-2006 01:02 PM
Build a Better Blair (like Build a Better Bush, only better) Kenny Computer Support 0 05-06-2005 04:50 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57