I would like to execute some code at the point
when the ‘class<<txt’ is taking place…
def txt.singleton_method_added(mthd) … end
is not exactly what you want, but close…
Yes its close… I wonder if there is anything closer?
Otherwise I have place a compare inside ‘singleton_method_added’
which checks if its first time we are being invoked.
Thanks for the hint.
ruby b.rb
singleton singleton_method_added
singleton aa
singleton bb
cat b.rb
txt = “text”
class << txt
def singleton_method_added(parent)
puts “singleton #{parent}”
end
def aa
end
def bb
end
end
At Thu, 4 Mar 2004 22:59:45 +0900,
Simon Strandgaard wrote in [ruby-talk:94219]:
I would like to execute some code at the point
when the ‘class<<txt’ is taking place…
But I cannot figure out how ?
I don’t know what you really want…
txt = “text”
class << txt
puts “like this?”
Yes… however if I invoke ‘install’ instead of above ‘puts’,
then
b.rb:3: undefined local variable or method `install’ for #<Class:#String:0x810a684> (NameError)
ruby b.rb
nil
b.rb:9: warning: instance variable @state not initialized
change state old= new=true
true
true
cat b.rb
obj = “im just some random object”
class << obj #install # why doesn’t this work?
def install
puts “install” @state = false
end
attr_reader :state
def change_state(state)
puts “change state old=#{@state} new=#{state}” @state = state
end
end
p obj.state
p obj.change_state(true)
p obj.state
···
On Fri, 05 Mar 2004 00:08:45 +0900, nobu.nokad wrote:
I would like to execute some code at the point
when the ‘class<<txt’ is taking place…
def txt.singleton_method_added(mthd) … end
is not exactly what you want, but close…
Yes its close… I wonder if there is anything closer?
Otherwise I have place a compare inside ‘singleton_method_added’
which checks if its first time we are being invoked.
Said so, done so… Its not pretty. I keep wondering why
Module#extended isn’t being invoked at this point?
Perhaps an RCR will cut it ?
ruby b.rb
singleton singleton_method_added
cat b.rb
txt = “text”
class << txt
alias old singleton_method_added
def singleton_method_added(parent)
puts “singleton #{parent}”
alias singleton_method_added old
undef old
end
def aa
end
def bb
end
end
···
On Thu, 04 Mar 2004 15:47:08 +0100, Simon Strandgaard wrote:
Yes… however if I invoke ‘install’ instead of above ‘puts’,
then
b.rb:3: undefined local variable or method `install’ for #<Class:#String:0x810a684> (NameError)
ruby b.rb
nil
b.rb:9: warning: instance variable @state not initialized
change state old= new=true
true
true
cat b.rb
obj = “im just some random object”
class << obj #install # why doesn’t this work?
def install
puts “install” @state = false
end
attr_reader :state
def change_state(state)
puts “change state old=#{@state} new=#{state}” @state = state
end
end
p obj.state
p obj.change_state(true)
p obj.state
There are two things standing in your way here:
If you do this:
class
my_method
end
then you’re calling a method on the Class object itself. (Note that
“” can be a constant or a “<< obj” expression. Both of
these forms do essentially the same thing, namely create and/or open
up a particular class.)
You can’t call a method before you define it. Mind you, you
haven’t actually defined it (the class method install, that
is).
obj = Object.new
class << obj
def self.install
#…
end
install
end
David
P.S. I have no idea if you’ll get this, since the news gateway still
isn’t working and I don’t know if you’re on ruby-talk. Sigh. I
believe it’s being looked into.