class<<inst -> neither included nor extended works

  • Thread starter Simon Strandgaard
  • Start date
S

Simon Strandgaard

I would like to execute some code at the point
when the 'class<<txt' is taking place..

But I cannot figure out how ?

server> ruby a.rb
server> cat a.rb
txt = "text"
class << txt
def included(parent)
puts "included"
end
def extended(parent)
puts "extended"
end
def self.included(parent)
puts "self.included"
end
def self.extended(parent)
puts "self.extended"
end
end
server>
 
C

Carlos

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...
 
S

Simon Strandgaard

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.


server> ruby b.rb
singleton singleton_method_added
singleton aa
singleton bb
server> cat b.rb
txt = "text"
class << txt
def singleton_method_added(parent)
puts "singleton #{parent}"
end
def aa
end
def bb
end
end
server>
 
S

Simon Strandgaard

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 ? :)


server> ruby b.rb
singleton singleton_method_added
server> 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
server>
 
N

nobu.nokada

Hi,

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?"
 
S

Simon Strandgaard

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)


server> ruby b.rb
nil
b.rb:9: warning: instance variable @state not initialized
change state old= new=true
true
true
server> 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
server>
 

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
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top