Hi
class A
...
end
x=:A.to_class # how can I convert the symbol (or string) of the classname to the class ?
o=x.new # == A.new
thanks
Opti
Hi
class A
...
end
x=:A.to_class # how can I convert the symbol (or string) of the classname to the class ?
o=x.new # == A.new
thanks
Opti
How about Object.const_get(:A)?
irb(main):001:0> class A
irb(main):002:1> def initialize(foo)
irb(main):003:2> puts foo
irb(main):004:2> end
irb(main):005:1> end
=> :initialize
irb(main):006:0> Object.const_get(:A).new("Bar")
Bar
=> #<A:0x007fbabbaef740>
On Sun, 22 Jan 2017 at 00:27 Die Optimisten <inform@die-optimisten.net> wrote:
Hi
class A
...
endx=:A.to_class # how can I convert the symbol (or string) of the
classname to the class ?o=x.new # == A.new
thanks
OptiUnsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Please do not post code that doesn't work; `to_class' does not exist
(and the class definition gives a syntax error).
If you just want to assign the class to a variable you do not even
need to use a symbol or string, you can do it directly:
class A
# ...
end
x = A
x.new # => #<A:0x0000...>
Or are you trying to do something different?
Regards,
Marcus
Am 22.01.2017 um 01:27 schrieb Die Optimisten:
class A
...
endx=:A.to_class # how can I convert the symbol (or string) of the
classname to the class ?o=x.new # == A.new
--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A
Opti, note also, that a symbol is not enough to do the conversion, you
also need a namespace for that. Or you restrict yourself to classes
and modules in top level namespace.
Cheers
robert
On Sun, Jan 22, 2017 at 9:23 AM, <sto.mar@web.de> wrote:
If you just want to assign the class to a variable you do not even
need to use a symbol or string, you can do it directly:class A
# ...
endx = A
x.new # => #<A:0x0000...>
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/
Hi
The problem is the conversion - how can I change a symbol to a class-name?
class A
end
x=:A
I'm looking to sg like y=Class(x) or y=x.to_class
Perhaps also witch x="NN::A"
thanks
Opti
On 2017-01-22 13:50, Robert Klemme wrote:
On Sun, Jan 22, 2017 at 9:23 AM, <sto.mar@web.de> wrote:
If you just want to assign the class to a variable you do not even
need to use a symbol or string, you can do it directly:class A
# ...
endx = A
x.new # => #<A:0x0000...>Opti, note also, that a symbol is not enough to do the conversion, you
also need a namespace for that. Or you restrict yourself to classes
and modules in top level namespace.Cheers
robert
The problem is the conversion - how can I change a symbol to a class-name?
Why do you need that? Why work with class names when you can use the
class object itself?
class A
end
x=:A
I'm looking to sg like y=Class(x) or y=x.to_class
Perhaps also witch x="NN::A"
Why?
robert
On Mon, Jan 23, 2017 at 10:51 AM, Die Optimisten <inform@die-optimisten.net> wrote:
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/
Remember that a "class name" is just a constant whose value is the Class
in question. So Jonathan's earlier answer is the one.
On 23 January 2017 at 19:51, Die Optimisten <inform@die-optimisten.net> wrote:
On 2017-01-22 13:50, Robert Klemme wrote:
On Sun, Jan 22, 2017 at 9:23 AM, <sto.mar@web.de> wrote:
If you just want to assign the class to a variable you do not even
need to use a symbol or string, you can do it directly:
class A
# ...
endx = A
x.new # => #<A:0x0000...>Opti, note also, that a symbol is not enough to do the conversion, you
also need a namespace for that. Or you restrict yourself to classes
and modules in top level namespace.Cheers
robert
Hi
The problem is the conversion - how can I change a symbol to a class-name?
class A
end
x=:A
I'm looking to sg like y=Class(x) or y=x.to_class
Perhaps also witch x="NN::A"thanks
Opti
~~~
y = Object.const_get(:A)
~~~
To make it work within a scope (a "namespace") you could probably do
something like these, but I've not tried them as I don't find it useful or
interesting:
~~~
y = NN.const_get(:A)
# or
z = Object.const_get('NN:A')
~~~
See Class: Module (Ruby 2.4.0)
Cheers
--
Matthew Kerwin
http://matthew.kerwin.net.au/
I want to create objects from a class, which is determined in runtime.
(if the student wants a square, it's inherited from Square; I don't know what he will want, and what will exist)
Will see, if that makes sense...
Opti
On 2017-01-23 13:50, Robert Klemme wrote:
On Mon, Jan 23, 2017 at 10:51 AM, Die Optimisten > <inform@die-optimisten.net> wrote:
The problem is the conversion - how can I change a symbol to a class-name?
Why do you need that? Why work with class names when you can use the
class object itself?class A
end
x=:A
I'm looking to sg like y=Class(x) or y=x.to_class
Perhaps also witch x="NN::A"
Hi,
I tried out of curiosity.
irb(main):006:0> def get_class(name)
irb(main):007:1> Kernel.const_get(name)
irb(main):008:1> rescue
irb(main):009:1> nil
irb(main):010:1> end
=> :get_class
irb(main):011:0> class Foo;end
=> nil
irb(main):012:0> klass = get_class(:Foo)
=> Foo
irb(main):013:0> klass.new
=> #<Foo:0x3d46aa8>
irb(main):014:0> get_class("Boo")
=> nil
Regards,
Toshi
And how is the class determined at runtime?
Cheers
robert
On Mon, Jan 23, 2017 at 9:37 PM, Die Optimisten <inform@die-optimisten.net> wrote:
On 2017-01-23 13:50, Robert Klemme wrote:
On Mon, Jan 23, 2017 at 10:51 AM, Die Optimisten >> <inform@die-optimisten.net> wrote:
The problem is the conversion - how can I change a symbol to a
class-name?Why do you need that? Why work with class names when you can use the
class object itself?class A
end
x=:A
I'm looking to sg like y=Class(x) or y=x.to_class
Perhaps also witch x="NN::A"I want to create objects from a class, which is determined in runtime.
(if the student wants a square, it's inherited from Square; I don't know
what he will want, and what will exist)
Will see, if that makes sense...
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/
I don't see how symbols come in here.
To create the objects the class would obviously need to exist,
so why not use the class name directly?
Regards,
Marcus
Am 23.01.2017 um 21:37 schrieb Die Optimisten:
I want to create objects from a class, which is determined in runtime.
(if the student wants a square, it's inherited from Square; I don't know
what he will want, and what will exist)
--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A