Define_method for a single instance?


#1

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

#2

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


#3

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 methodwibble’ for #<B:0x1e88ec>


#4

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


#5

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

#6

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.