Classes dynamic loading

hi list,
i ve been learning ruby from 2 days.
i found it at impact a faboulous language.

i would implement something like this:

in the main file
class Test
def msg
puts 'to overload’
end
end

t1 = Test.new
t2 = Test.new

class <<t1
load 'myclass’
end

class <<t2
load 'myclass2’
end

then in myclassN
def msg
puts 'overloadedN’
end

how can i do that in your opinion?
tnx.

hi list,
i ve been learning ruby from 2 days.
i found it at impact a faboulous language.

i would implement something like this:

in the main file
class Test
def msg
puts ‘to overload’
end
end

t1 = Test.new
t2 = Test.new

class <<t1
load ‘myclass’
end

class <<t2
load ‘myclass2’
end

then in myclassN
def msg
puts ‘overloadedN’
end

how can i do that in your opinion?
tnx.

Ooh! Ooh! I know this one!

0rasputin@lb:rasputin$ ruby dyn.rb
before overload
to overload
to overload
after overload:
overload1
overload2
0rasputin@lb:rasputin$ expand -t2 dyn.rb
class Test
def msg
puts ‘to overload’
end
end

t1 = Test.new
t2 = Test.new

puts ‘before overload’
t1.msg ; t2.msg

class << t1
def msg
puts ‘overload1’
end
end

class << t2
def msg
puts ‘overload2’
end
end

puts ‘after overload:’
t1.msg ; t2.msg
0rasputin@lb:rasputin$

···


Fifth Law of Procrastination:
Procrastination avoids boredom; one never has the feeling that
there is nothing important to do.
Rasputin :: Jack of All Trades - Master of Nuns

<- snip ->

class << t1
def msg
puts 'overload1’
end
end

class << t2
def msg
puts 'overload2’
end
end

puts 'after overload:'
t1.msg ; t2.msg
0rasputin@lb:rasputin$

<- snip ->

i need to load the classes methods to overload from a file,
and not the same one :slight_smile:

i know ruby can ‘load’ source code but in this case it will fail.
i wold know why and what is the right solution.

tnx for your reply

i need to load the classes methods to overload from a file,
and not the same one :slight_smile:

i know ruby can ‘load’ source code but in this case it will fail.
i wold know why and what is the right solution.

This probably needs more tweaking.
Here is 2 files ‘a.rb’ and ‘modules.rb’

ruby a.rb
to overload
to overload
overloaded1
overloaded2
cat a.rb
class Test
def msg
puts ‘to overload’
end
end
t1 = Test.new
t2 = Test.new
t1.msg
t2.msg
require ‘modules’
t1.extend(Module1)
t2.extend(Module2)
t1.msg
t2.msg
cat modules.rb
module Module1
def msg
puts ‘overloaded1’
end
end
module Module2
def msg
puts ‘overloaded2’
end
end

BTW: welcome to Ruby :wink:

···

On Tue, 30 Mar 2004 05:23:28 +0900, saiph wrote:


Simon Strandgaard