Forum: Ruby write a method to every possible class

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7d0f39ba054669bda3c73ab58465d37f?d=identicon&s=25 unknown (Guest)
on 2009-03-25 10:26
(Received via mailing list)
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...
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2009-03-25 10:50
(Received via mailing list)
Hi,

2009/3/25, oguzaltu@gmail.com <oguzaltu@gmail.com>:
> 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
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-03-25 11:00
(Received via mailing list)
On Wed, Mar 25, 2009 at 10:21 AM,  <oguzaltu@gmail.com> wrote:
> 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.
This topic is locked and can not be replied to.