Forum: Ruby define_method for a single instance?

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.
Jake McArthur (Guest)
on 2006-05-24 22:19
(Received via mailing list)
I'm trying to define a method for a specific instance of a class
rather than for every instance of the class. Basically, something
like this:

class A
   def add_wibble_method
     # don't know what to do here
   end
end

foo = A.new
bar = A.new
foo.add_wibble_method
foo.wibble # should work
# bar.wibble should not work

Any help would be appreciated.

- Jake McArthur
Mike F. (Guest)
on 2006-05-24 22:33
Jake McArthur wrote:
> I'm trying to define a method for a specific instance of a class
> rather than for every instance of the class. Basically, something
> like this:
>
> class A
>    def add_wibble_method
>      # don't know what to do here
>    end
> end
>
> foo = A.new
> bar = A.new
> foo.add_wibble_method
> foo.wibble # should work
> # bar.wibble should not work

module A
  def A.add_wibble_method( obj )
    class << obj
      define_method( :wibble ) { puts "Wibble!" }
    end
  end
end

k = Object.new
A.add_wibble_method( k )

k.wibble
Logan C. (Guest)
on 2006-05-24 22:39
(Received via mailing list)
On May 24, 2006, at 2:18 PM, Jake McArthur wrote:

> foo = A.new
> bar = A.new
> foo.add_wibble_method
> foo.wibble # should work
> # bar.wibble should not work
>
> Any help would be appreciated.
>
> - Jake McArthur
>

% cat wibble.rb
class A
   def add_wibble
     def self.wibble
       puts "I'm wibbling!"
     end
   end
end

class B
   def add_wibble
     (class << self; self; end).class_eval do
       define_method(:wibble) { puts "I too can wibble." }
     end
   end
end

a = A.new
b = B.new
a.add_wibble
a.wibble
b.add_wibble
b.wibble
begin
a1 = A.new
a1.wibble
rescue => ex
   puts ex
end

begin
   b1 = B.new
   b1.wibble
rescue => ex
   puts ex
end




% ruby wibble.rb
I'm wibbling!
I too can wibble.
undefined method `wibble' for #<A:0x1e89f0>
undefined method `wibble' for #<B:0x1e88ec>
Ryan L. (Guest)
on 2006-05-24 22:42
(Received via mailing list)
On 5/24/06, Jake McArthur <removed_email_address@domain.invalid> wrote:
> foo = A.new
> bar = A.new
> foo.add_wibble_method
> foo.wibble # should work
> # bar.wibble should not work
>
> Any help would be appreciated.

irb(main):001:0> class A
irb(main):002:1>   def add_wibble_method
irb(main):003:2>     class << self
irb(main):004:3>       def wibble
irb(main):005:4>         puts "wibble!"
irb(main):006:4>       end
irb(main):007:3>     end
irb(main):008:2>   end
irb(main):009:1> end
=> nil
irb(main):010:0> foo = A.new
=> #<A:0x2ac53f0>
irb(main):011:0> bar = A.new
=> #<A:0x2ac2ed0>
irb(main):012:0> foo.add_wibble_method
=> nil
irb(main):013:0> foo.wibble
wibble!
=> nil
irb(main):014:0> bar.wibble
NoMethodError: undefined method `wibble' for #<A:0x2ac2ed0>
        from (irb):14
Jake McArthur (Guest)
on 2006-05-24 22:48
(Received via mailing list)
On May 24, 2006, at 1:37 PM, Logan C. wrote:

> class B
>   def add_wibble
>     (class << self; self; end).class_eval do
>       define_method(:wibble) { puts "I too can wibble." }
>     end
>   end
> end

Perfect! Thanks.

- Jake McArthur
Elliot T. (Guest)
on 2006-05-24 23:39
(Received via mailing list)
On May 24, 2006, at 11:18 AM, Jake McArthur wrote:

> foo = A.new
> bar = A.new
> foo.add_wibble_method
> foo.wibble # should work
> # bar.wibble should not work
>
> Any help would be appreciated.

#the self is needed or bar gets wibble too.

class A
   def add_wibble_method
     # don't know what to do here
     def self.wibble
       puts "wibble"
     end
   end
end

foo = A.new
bar = A.new
foo.add_wibble_method
foo.wibble # should work
bar.wibble rescue puts "error"


-- Elliot T.
http://www.curi.us/blog/
This topic is locked and can not be replied to.