Metaprograming

  • Thread starter Sateesh Kambhamapati
  • Start date
S

Sateesh Kambhamapati

i would like to know which methods are called by the object created by
class with in class.
If Suppose if particular method is not there it invokes
method_missing.But i want it invoke directly my class .so i would like
create that method dynamically
eg:
class Flower
def rose
puts "rose is beatiful"
end
end
f=flower.new
f.rose-------------------->It gives me rose
but now f.jasmine------------------->It calls method_missing
but i want it comes to my class "Flower"

So i Want to know which methods are invoked by my "Flower" object
Please help me
 
J

Jesús Gabriel y Galán

i would like to know which methods are called by the object created by
class with in class.
If Suppose if particular method is not there it invokes
method_missing.But i want it invoke directly my class .so i would like
create that method dynamically
eg:
class Flower
def rose
puts "rose is beatiful"
end
end
f=flower.new
f.rose-------------------->It gives me rose
but now f.jasmine------------------->It calls method_missing
but i want it comes to my class "Flower"

I'm not sure if I understand you correctly, but if you want those
method missing calls, to be called on the Flower class, you can do
this:

irb(main):001:0> class Flower
irb(main):002:1> def self.jasmine
irb(main):003:2> "jasmine"
irb(main):004:2> end
irb(main):005:1> def method_missing meth, *args, &blk
irb(main):006:2> self.class.send meth, *args, &blk
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):016:0> Flower.new.jasmine
=> "jasmine"
So i Want to know which methods are invoked by my "Flower" object

I don't understand this question.

Jesus.
 
S

Sateesh Kambhamapati

Jesús Gabriel y Galán said:
I'm not sure if I understand you correctly, but if you want those
method missing calls, to be called on the Flower class, you can do
this:

irb(main):001:0> class Flower
irb(main):002:1> def self.jasmine
irb(main):003:2> "jasmine"
irb(main):004:2> end
irb(main):005:1> def method_missing meth, *args, &blk
irb(main):006:2> self.class.send meth, *args, &blk
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):016:0> Flower.new.jasmine
=> "jasmine"


I don't understand this question.

Jesus.

Thanks for reply
i would like to know which methods are called by class object i.e
class Flower
def jasmine
puts "iam jasmine"
end
def rose
puts "iam rose"
end
end

flowerlist=Flower.new #----->Creating instance for flower
flowerlist.jasmine #------->"gives output as "iam jasmine"
flowerlist.rose # =======> "gives output as "i am rose"
flowerlist.lotus #------->"Shows No Method Error"

Question : --> My Question is i would like to know which methods are
called by object i.e flowerlist in the class Flower

if i already know those methods then
suppose if i have not define some method then i will define it
dynamically example here is "lotus"

i think u understand my question
pleasee gave me reply
 
P

Phrogz

flowerlist=Flower.new #----->Creating instance for flower
flowerlist.jasmine  #------->"gives output as "iam jasmine"
flowerlist.rose  # =======> "gives output as "i am rose"
flowerlist.lotus   #------->"Shows No Method Error"

Question : --> My Question is i would like to know which methods are
called by object i.e flowerlist  in the class Flower

if i already know those methods then
suppose if i have not define some method then i will define it
dynamically example here is "lotus"

i think u understand my question
pleasee gave me reply

I don't understand your question yet, but perhaps the following will
help. If this is not what you meant, please post exactly the code you
would like to write, and what results you would like from that code.



module AutoMethodMaker
def self.included(base)
base.extend(ClassMethods)
end
def method_missing(method_name,*args)
if self.class.maybe_make_method(method_name,*args)
self.send(method_name,*args)
end
end
module ClassMethods
def methods_made
@methods_made ||= []
end

def auto_make_methods(pattern,&block)
(@auto_make_methods ||= {})[ pattern ] = block
end

def maybe_make_method(method_name,*args)
@auto_make_methods.each do |pattern,block|
if pattern===method_name
define_method(method_name,&block)
methods_made << method_name
return true
end
end
end

end
end

class Flower
include AutoMethodMaker
auto_make_methods /special_.+/ do
puts "Special flower #{__method__[/special_(.+)/,1]}!!"
end
auto_make_methods /.+/ do
puts "I am a #{__method__}"
end
def standard
puts "I am a built-in method."
end
end

f = Flower.new
p Flower.methods_made #=> []
f.jasmine #=> I am a jasmine
f.rose #=> I am a rose
f.rose #=> I am a rose
f.special_rose #=> Special flower rose!!
f.standard #=> I am a built-in method.
f.lotus #=> I am a lotus
p Flower.methods_made #=> [:jasmine, :rose, :special_rose, :lotus]
 
J

Jesús Gabriel y Galán

Thanks for reply
i would like to know which methods are called by class object i.e
class Flower
=A0 =A0 def jasmine
=A0 =A0 puts "iam jasmine"
=A0 end
=A0 =A0 =A0def rose
=A0 =A0 =A0puts "iam rose"
=A0end
end

flowerlist=3DFlower.new #----->Creating instance for flower
flowerlist.jasmine =A0#------->"gives output as "iam jasmine"
flowerlist.rose =A0# =3D=3D=3D=3D=3D=3D=3D> "gives output as "i am rose"
flowerlist.lotus =A0 #------->"Shows No Method Error"

Question : --> My Question is i would like to know which methods are
called by object i.e flowerlist =A0in the class Flower

if i already know those methods then
suppose if i have not define some method then i will define it
dynamically example here is "lotus"

i think u understand my question

As I understand the question now, I think the answer from Phrogz is
spot on. You need to use the method_missing method, which will catch
any call to a method you haven't defined:

class Flower
def jasmine
puts "iam jasmine"
end
def rose
puts "iam rose"
end

def method_missing meth, *args, &blk
puts "method #{meth} called, but not defined"
end
end

flowerlist=3DFlower.new #----->Creating instance for flower
flowerlist.jasmine #------->"gives output as "iam jasmine"
flowerlist.rose # =3D=3D=3D=3D=3D=3D=3D> "gives output as "i am rose"
flowerlist.lotus #=3D> "method lotus called, but not defined"

So, now you know where you have a place to do whatever you want to do
with a call to a method that doesn't exist. For example, if you want
to dynamically create a method that returns its name as a string, you
can do:

def method_missing method, *args, &blk
self.class.send:)define_method, method) do
method.to_s
end
send(method)
end

This will dynamically define (and call) a method that returns its name
as a string. So if you add that to the Flower class:

class Flower
def method_missing method, *args, &blk
self.class.send:)define_method, method) do
method.to_s
end
send(method)
end
end

f =3D Flower.new
f.lotus #=3D> "lotus"
f.rose #=3D> "rose"

and so on. In any case, I recommend looking at Phrogz's answer,
because it's a nice little utility to automatically define method that
do different things depending the the name patterns.

Regards,

Jesus.
 
B

Brian Candler

Sateesh said:
i would like to know which methods are called by class object i.e

Not sure what you mean by "called by class object"
Question : --> My Question is i would like to know which methods are
called by object i.e flowerlist in the class Flower

if i already know those methods then
suppose if i have not define some method then i will define it
dynamically example here is "lotus"

If you want to intercept *all* calls to the object, then put a proxy
object in front of it, and delegate the calls (e.g. see
SimpleDelegator).

If you're happy to intercept only the unknown methods (which you will
then define), then method_missing is what you want.

class Flower
def jasmine
puts "I am jasmine"
end
def method_missing(meth,*args,&blk)
puts "method #{meth.inspect} called with #{args.inspect}"
end
end
flowerlist=Flower.new
flowerlist.jasmine # I am jasmine
flowerlist.lotus # method :lotus called with []
 
J

Jesús Gabriel y Galán

My goodness. Has anyone asked why such metaprogramming might be necessary? I
don't even understand the question, let enough to the extent that I would
feel confident in suggesting such drastic measures as the solution.

It's what I understood from the OP. He said:

"suppose if i have not define some method then i will define it dynamically"

From this and the rest of his question, I understood he wanted to
intercept calls to missing methods and define them dynamically.
Do you understand something different?
Metaprogramming is not a universal hammer. Even if it were, you wouldn't
want everything to be a nail.

I can agree with this, but when the OP explicitly asks for a way to
dynamically define missing methods, this is what comes to my mind.

Of course, it might very well be the case that to solve OP's problem,
this is not necessary at all and that studying his original problem
might lead to a different solution, but in any case I see no harm in
explaining him method_missing and how to define methods. Feel free to
ask him about what problem he is trying to solve, and maybe we can
come up with a better solution.

Regards,

Jesus.
 
J

Jesús Gabriel y Galán

Agreed. I would prefer that we attempt to understand a bit more of the
context of the question.

Rant follows:

Questions like these are often the result of something like the
Dunning=96Kruger effect, where the questioner may not know enough to know= that
the question is bad and may only receive a confusing or misleading answer=
 

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
474,149
Messages
2,570,842
Members
47,388
Latest member
EarthaGilm

Latest Threads

Top