S
Sean Ross
Hi.
In Ruby, if I want to make an instance of a class C, the syntax is
c = C.new
But to define how that instance is initialized I need to make a method
called "initialize", not "new". Like so:
class C
attr_reader :a
def initialize
@a = "a"
end
end
I was curious, is there a way to do the initialization in a method "C.new"
without defining an "initialize" method?
Something like this:
class C
attr_reader :a
def C.new
# initialization
# return new instance of C
end
end
Why? Mostly curiousity. Ever since I've noticed the assymmetry between the
initialization syntax (c=C.new) and the initialization definition (def
initialize rather than def C.new) I've wondered if it would be possible to
use a more symmetrical method.
I realize that this is completely unneccessary and that people are happy
with the mechanisms that are in place, it's just that, for me, this a bit of
an itch to scratch.
Thank you for your time and attention,
Sean
In Ruby, if I want to make an instance of a class C, the syntax is
c = C.new
But to define how that instance is initialized I need to make a method
called "initialize", not "new". Like so:
class C
attr_reader :a
def initialize
@a = "a"
end
end
I was curious, is there a way to do the initialization in a method "C.new"
without defining an "initialize" method?
Something like this:
class C
attr_reader :a
def C.new
# initialization
# return new instance of C
end
end
Why? Mostly curiousity. Ever since I've noticed the assymmetry between the
initialization syntax (c=C.new) and the initialization definition (def
initialize rather than def C.new) I've wondered if it would be possible to
use a more symmetrical method.
I realize that this is completely unneccessary and that people are happy
with the mechanisms that are in place, it's just that, for me, this a bit of
an itch to scratch.
Thank you for your time and attention,
Sean