[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

method_missing question

Joe Van Dyk

4/29/2005 5:55:00 PM

Currently, I have a class that has a bunch of methods (that a GUI does
a callback on) that are like

def menu_zoom_100_activate
@display.zoom(100)
end

def menu_zoom_250_activate
@display.zoom(250)
end

def menu_zoom_500_activate
@display.zoom(500)
end

Could I use #method_missing to make this better?



10 Answers

Rick Olson

4/29/2005 5:58:00 PM

0

Why not just call @display.zoom(500)?

On 4/29/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> Currently, I have a class that has a bunch of methods (that a GUI does
> a callback on) that are like
>
> def menu_zoom_100_activate
> @display.zoom(100)
> end
>
> def menu_zoom_250_activate
> @display.zoom(250)
> end
>
> def menu_zoom_500_activate
> @display.zoom(500)
> end
>
> Could I use #method_missing to make this better?
>
>


--
rick
http://techno-...



Joe Van Dyk

4/29/2005 6:03:00 PM

0

On 4/29/05, Rick Olson <technoweenie@gmail.com> wrote:
> Why not just call @display.zoom(500)?
>
> On 4/29/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> > Currently, I have a class that has a bunch of methods (that a GUI does
> > a callback on) that are like
> >
> > def menu_zoom_100_activate
> > @display.zoom(100)
> > end
> >
> > def menu_zoom_250_activate
> > @display.zoom(250)
> > end
> >
> > def menu_zoom_500_activate
> > @display.zoom(500)
> > end
> >
> > Could I use #method_missing to make this better?
> >

Because when a GUI button is clicked, I need to call a particular
method of a particular class. I don't think I can give it an
argument.



Joe Van Dyk

4/29/2005 6:12:00 PM

0

On 4/29/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> On 4/29/05, Rick Olson <technoweenie@gmail.com> wrote:
> > Why not just call @display.zoom(500)?
> >
> > On 4/29/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> > > Currently, I have a class that has a bunch of methods (that a GUI does
> > > a callback on) that are like
> > >
> > > def menu_zoom_100_activate
> > > @display.zoom(100)
> > > end
> > >
> > > def menu_zoom_250_activate
> > > @display.zoom(250)
> > > end
> > >
> > > def menu_zoom_500_activate
> > > @display.zoom(500)
> > > end
> > >
> > > Could I use #method_missing to make this better?
> > >
>
> Because when a GUI button is clicked, I need to call a particular
> method of a particular class. I don't think I can give it an
> argument.
>

Actually, (I'm using Glade) I have this:

app_window = AppWindow.new
GladeXML.new("app.glade") { |handler| app_window.method(handler) }

Where AppWindow is a class that contains all the methods that are
defined in the glade file. In the glade file are methods being
registered like "menu_zoom_250_activate", "menu_zoom_500_activate" and
so on.

If a particular method that's contained in the glade file isn't
defined in the AppWindow class, I get a "undefined method '<some
method>' for class AppWindow" upon the start of the application.

I added AppWindow#method_missing that just prints out the missing
method, but that didn't seem to solve anything.

Perhaps it would be best to have a single method in AppWindow that
took all the methods (as a string) that were defined in the glade file
and executed the appropriate action?

So,

GladeXML.new("app.glade") { |handler| app_window.execute_method(handler) }

class AppWindow
def execute_method(method)
case method
when /menu_zoom_(\d+)_activate/
@display.zoom($1.to_i)
when "something_else"
# do something else
end
end
end



Joe Van Dyk

4/29/2005 6:16:00 PM

0

On 4/29/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> On 4/29/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> > On 4/29/05, Rick Olson <technoweenie@gmail.com> wrote:
> > > Why not just call @display.zoom(500)?
> > >
> > > On 4/29/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> > > > Currently, I have a class that has a bunch of methods (that a GUI does
> > > > a callback on) that are like
> > > >
> > > > def menu_zoom_100_activate
> > > > @display.zoom(100)
> > > > end
> > > >
> > > > def menu_zoom_250_activate
> > > > @display.zoom(250)
> > > > end
> > > >
> > > > def menu_zoom_500_activate
> > > > @display.zoom(500)
> > > > end
> > > >
> > > > Could I use #method_missing to make this better?
> > > >
> >
> > Because when a GUI button is clicked, I need to call a particular
> > method of a particular class. I don't think I can give it an
> > argument.
> >
>
> Actually, (I'm using Glade) I have this:
>
> app_window = AppWindow.new
> GladeXML.new("app.glade") { |handler| app_window.method(handler) }
>
> Where AppWindow is a class that contains all the methods that are
> defined in the glade file. In the glade file are methods being
> registered like "menu_zoom_250_activate", "menu_zoom_500_activate" and
> so on.
>
> If a particular method that's contained in the glade file isn't
> defined in the AppWindow class, I get a "undefined method '<some
> method>' for class AppWindow" upon the start of the application.
>
> I added AppWindow#method_missing that just prints out the missing
> method, but that didn't seem to solve anything.
>
> Perhaps it would be best to have a single method in AppWindow that
> took all the methods (as a string) that were defined in the glade file
> and executed the appropriate action?
>
> So,
>
> GladeXML.new("app.glade") { |handler| app_window.execute_method(handler) }
>
> class AppWindow
> def execute_method(method)
> case method
> when /menu_zoom_(\d+)_activate/
> @display.zoom($1.to_i)
> when "something_else"
> # do something else
> end
> end
> end
>

Nuts, I tried that, and AppWindow#execute_method is being run at
application start for each method defined in the glade file, and now
none of the GUI events do anything. Hm.



Ryan Leavengood

4/29/2005 7:02:00 PM

0

Joe Van Dyk wrote:
>
> Could I use #method_missing to make this better?

Yep. Try this out:

class Display
def zoom(i)
puts "Zooming to #{i}"
end
end

class AppWindow
def initialize()
@display = Display.new
end

def method_missing(method, *args)
case method.id2name
when /menu_zoom_(\d+)_activate/
@display.zoom($1.to_i)
else
puts "Unknown method: #{method}"
end
end
end

if $0 == __FILE__
app = AppWindow.new
app.menu_zoom_100_activate
app.menu_zoom_250_activate
app.menu_zoom_500_activate
app.blah
end


Curt Hibbs

4/29/2005 7:11:00 PM

0

Joe Van Dyk wrote:
> On 4/29/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
>
>>On 4/29/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
>>
>>>On 4/29/05, Rick Olson <technoweenie@gmail.com> wrote:
>>>
>>>>Why not just call @display.zoom(500)?
>>>>
>>>>On 4/29/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
>>>>
>>>>>Currently, I have a class that has a bunch of methods (that a GUI does
>>>>>a callback on) that are like
>>>>>
>>>>> def menu_zoom_100_activate
>>>>> @display.zoom(100)
>>>>> end
>>>>>
>>>>> def menu_zoom_250_activate
>>>>> @display.zoom(250)
>>>>> end
>>>>>
>>>>> def menu_zoom_500_activate
>>>>> @display.zoom(500)
>>>>> end
>>>>>
>>>>>Could I use #method_missing to make this better?
>>>>>
>>>
>>>Because when a GUI button is clicked, I need to call a particular
>>>method of a particular class. I don't think I can give it an
>>>argument.
>>>
>>
>>Actually, (I'm using Glade) I have this:
>>
>>app_window = AppWindow.new
>>GladeXML.new("app.glade") { |handler| app_window.method(handler) }
>>
>>Where AppWindow is a class that contains all the methods that are
>>defined in the glade file. In the glade file are methods being
>>registered like "menu_zoom_250_activate", "menu_zoom_500_activate" and
>>so on.
>>
>>If a particular method that's contained in the glade file isn't
>>defined in the AppWindow class, I get a "undefined method '<some
>>method>' for class AppWindow" upon the start of the application.
>>
>>I added AppWindow#method_missing that just prints out the missing
>>method, but that didn't seem to solve anything.
>>
>>Perhaps it would be best to have a single method in AppWindow that
>>took all the methods (as a string) that were defined in the glade file
>>and executed the appropriate action?
>>
>>So,
>>
>>GladeXML.new("app.glade") { |handler| app_window.execute_method(handler) }
>>
>>class AppWindow
>> def execute_method(method)
>> case method
>> when /menu_zoom_(\d+)_activate/
>> @display.zoom($1.to_i)
>> when "something_else"
>> # do something else
>> end
>> end
>>end
>>
>
>
> Nuts, I tried that, and AppWindow#execute_method is being run at
> application start for each method defined in the glade file, and now
> none of the GUI events do anything. Hm.

How about going back to you method_missing idea, and then in
method_missing, parsing the name of the method that was being called and
converting that to a method call that you eval.

For example: if the method being called was "display_zoom_250", convert
that to the string "@display.zoom(250)" and then eval that string.

Curt


Pit Capitain

4/29/2005 7:18:00 PM

0

Joe Van Dyk schrieb:
>
>Currently, I have a class that has a bunch of methods (that a GUI does
>a callback on) that are like
>
> def menu_zoom_100_activate
> @display.zoom(100)
> end
>
> def menu_zoom_250_activate
> @display.zoom(250)
> end
>
> def menu_zoom_500_activate
> @display.zoom(500)
> end
>
>Could I use #method_missing to make this better?

You should be able to use method_missing, but you could also do
something like

class Handler

[ 100, 250, 500 ].each do |factor|
define_method("menu_zoom_#{factor}_activate") do
@display.zoom(factor)
end
end

end

or for some more syntax sugar define a class method

class Handler

class << self
def zoom_methods(*factors)
...see above...
end
end

zoom_methods 100, 250, 500

end

Regards,
Pit


Les Rennie

4/29/2005 9:27:00 PM

0

Joe Van Dyk wrote:
> Currently, I have a class that has a bunch of methods (that a GUI does
> a callback on) that are like
>
> def menu_zoom_100_activate
> @display.zoom(100)
> end
>
> def menu_zoom_250_activate
> @display.zoom(250)
> end
>
> def menu_zoom_500_activate
> @display.zoom(500)
> end
>
> Could I use #method_missing to make this better?
>
>
>

you could do that like this:

def method_missing(methodname, *args)
if methodname=~/menu_zoom(\d+)_activate/
@display.zoom($1.to_i)
else
super
end
end

Joe Van Dyk

4/30/2005 12:46:00 AM

0

On 4/29/05, Pit Capitain <pit@capitain.de> wrote:
> Joe Van Dyk schrieb:
> >
> >Currently, I have a class that has a bunch of methods (that a GUI does
> >a callback on) that are like
> >
> > def menu_zoom_100_activate
> > @display.zoom(100)
> > end
> >
> > def menu_zoom_250_activate
> > @display.zoom(250)
> > end
> >
> > def menu_zoom_500_activate
> > @display.zoom(500)
> > end
> >
> >Could I use #method_missing to make this better?
>
> You should be able to use method_missing, but you could also do
> something like
>
> class Handler
>
> [ 100, 250, 500 ].each do |factor|
> define_method("menu_zoom_#{factor}_activate") do
> @display.zoom(factor)
> end
> end
>
> end
>

DING DING DING.

The above worked perfectly. Thanks!



Stile4aly

3/25/2010 2:34:00 PM

0

On Mar 25, 7:24 am, wolfagain <w...@provide.net> wrote:
> On Mar 25, 8:40 am, Matt <matttel...@sprynet.com> wrote:
>
>
>
>
>
> > On Mar 25, 7:17 am, wolfagain <w...@provide.net> wrote:
>
> > > We have fewer and fewer people actually employed EVERY week, so of
> > > course there are fewer and fewer workers available to lose their jobs!
> > > ALWAYS be suspicious of any of Hopeless Harry's posts and the same
> > > with other LIARS who use figures....figures LIE and LIARS use figures!
>
> > Okay, why don't we look at the numbers, just to see which of you might
> > be right.
>
> > Total Civilian workforce:
>
> > Feb 2009: 154,401
> > Dec 2009: 153,059
> > Jan 2010: 153,170
> > Feb 2010: 153,512
>
> > All numbers in thousands, of course.
>
> > So, in fact, the number of people working has been going up this year.
> > Hm.
> > That would mean Harry was right and you were wrong.
>
> > Matt
>
> The Job loses are ALL from the private sector....Government jobs have
> increased about 8% since your boy took office...Census workers are but
> one example.- Hide quoted text -
>
> - Show quoted text -

That's demonstrably false.

http://www.bls.gov/news.release/emps...

According to the BLS, Government employment is up about 6/10ths of 1%
from Feb 09 to Feb 10.

"In February, employment in the federal government edged up. The
hiring of
15,000 temporary workers for Census 2010 was partially offset by a
decline
in U.S. Postal Service employment."