Class<<inst -> neither included nor extended works


(Simon Strandgaard) #1

I would like to execute some code at the point
when the ‘class<<txt’ is taking place…

But I cannot figure out how ?

ruby a.rb
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

···


Simon Strandgaard


(David A. Black) #2

Hi –

I would like to execute some code at the point
when the ‘class<<txt’ is taking place…

But I cannot figure out how ?

ruby a.rb
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

I’m not sure what you mean. Aren’t the 'def’s being executed?
txt = "text"
class << txt
def x; puts “hi”; end
end

txt.x # hi

David

···

On Thu, 4 Mar 2004, Simon Strandgaard wrote:


David A. Black
dblack@wobblini.net


(Carlos) #3

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…


(Nobuyoshi Nakada) #4

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?”

···


Nobu Nakada


(Simon Strandgaard) #5

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

···

On Thu, 04 Mar 2004 23:41:28 +0900, Carlos wrote:


Simon Strandgaard


(Simon Strandgaard) #6

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:


Simon Strandgaard


(Simon Strandgaard) #7

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

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:

On Thu, 04 Mar 2004 23:41:28 +0900, Carlos wrote:


Simon Strandgaard


(David A. Black) #8

Hi –

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:

  1. 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.)

  1. You can’t call a method before you define it. Mind you, you
    haven’t actually defined it :slight_smile: (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.

···

On Fri, 5 Mar 2004, Simon Strandgaard wrote:


David A. Black
dblack@wobblini.net