write a method to every possible class

O

oguzaltu

I have a function like this:

def fun(ob)
return a_modified_version_of_ob
end

Of course, if I have an object instance 'obis', I need to call it
like
fun(obis)
I want to be able to call like obis.fun
And I want to be able to do that all object types
Possible?

Thx...
 
H

Heesob Park

Hi,

2009/3/25 said:
I have a function like this:

def fun(ob)
return a_modified_version_of_ob
end

Of course, if I have an object instance 'obis', I need to call it
like
fun(obis)
I want to be able to call like obis.fun
And I want to be able to do that all object types
Possible?
You can define fun method under Object class like this:
class Object
def fun
# p self
return a_modified_version_of_self
end
end

Regards,

Park Heesob
 
J

Jesús Gabriel y Galán

I have a function like this:

def fun(ob)
return a_modified_version_of_ob
end

Of course, if I have an object instance 'obis', I need to call it
like
fun(obis)
I want to be able to call like obis.fun
And I want to be able to do that all object types
Possible?

Thx...

irb(main):010:0> class Object
irb(main):011:1> def fun
irb(main):012:2> @modified_flag = true #example of changing the object
irb(main):013:2> self
irb(main):014:2> end
irb(main):015:1> end
=> nil
irb(main):018:0> a = "abc".fun
=> "abc"
irb(main):019:0> a.instance_variables
=> ["@modified_flag"]
irb(main):020:0> class X
irb(main):021:1> end
=> nil
irb(main):022:0> X.new.fun
=> #<X:0xb7c0d318 @modified_flag=true>
irb(main):023:0>

Hope this helps,

Jesus.
 

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,176
Messages
2,570,950
Members
47,501
Latest member
log5Sshell/alfa5

Latest Threads

Top