A bit confused about Ruby’s initialize method.
Why does not it return my value, as other methods do?
class SomeClass
attr_reader :val
def initialize a, b, c
@val = a + b + c
@val
end
end
val = SomeClass.new 1, 2, 3
I need val to be 6 not initialized object.
Of course i can use val.val but that’s another story.
You need to override
self.newmethod:When you create a instance of a class you are in fact calling
self.newof that class which then callinitializemethod and return the initialized instance.See live demo here