Hello! Is there a way to include constants from one class into another?
Something like “include A::*”?
SAMPLE:
class A
class B; end
class C; end
class D; end
end
class E
# include A::*
B; C; D;
end
Hello! Is there a way to include constants from one class into another?
Something like “include A::*”?
SAMPLE:
class A
class B; end
class C; end
class D; end
end
class E
# include A::*
B; C; D;
end
2008/12/29 Alexey P. [email protected]:
class E
# include A::*
B; C; D;
end
robert@fussel ~
$ ruby <<EOF
D=:
end
p E::C.new
EOF
#<A::C:0x1002ebc0>
robert@fussel ~
$
You can as well do something like this
class Module
def import_all(mod)
mod.constants.each do |c|
const_set(c, mod.const_get(c))
end
end
end
irb(main):016:0> class E
irb(main):017:1> import_all A
irb(main):018:1> end
=> [“C”, “B”, “D”]
irb(main):019:0> E::B.new
=> #<A:0x7ff60548>
Kind regards
robert
Thanks!
On Dec 29, 3:32 pm, Alexey P. [email protected] wrote:
class E # include A::* B; C; D; end
–
Posted viahttp://www.ruby-forum.com/.
module Foo
class A; end
class B; end
class C; end
end
module Bar
include Foo
end
Bar::A.new
HTH,
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs