From: Sateesh Kambhamapati on

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
--
Posted via http://www.ruby-forum.com/.

From: Jesús Gabriel y Galán on
On Thu, Jun 10, 2010 at 11:53 AM, Sateesh Kambhamapati
<sateesh.mca09(a)gmail.com> wrote:
>
> 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.

From: Sateesh Kambhamapati on
Jesús Gabriel y Galán wrote:
> On Thu, Jun 10, 2010 at 11:53 AM, Sateesh Kambhamapati
> <sateesh.mca09(a)gmail.com> wrote:
>> 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.

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



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

From: Phrogz on
On Jun 13, 11:23 pm, Sateesh Kambhamapati <sateesh.mc...(a)gmail.com>
wrote:
> >> So i Want to know which methods are invoked by my "Flower" object
> > I don't understand this question.
> 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]
From: Raghu V. Hudli on
On 14/06/10 10:53 AM, Sateesh Kambhamapati wrote:
> Jesús Gabriel y Galán wrote:
>> On Thu, Jun 10, 2010 at 11:53 AM, Sateesh Kambhamapati
>> <sateesh.mca09(a)gmail.com> wrote:
>>> 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.
>
> 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
>
>
>

Are you asking for the list of methods defined on the Object class? If
so a simple 2 liner will give you the list of methods

obj = Object.new;
print obj.methods.sort.join("\n");

Raghu