class Object
attr_reader :foo
def initialize
@foo = 'bar'
end
end
Object.new.foo # => 'bar'
''.foo # => nil
//.foo # => nil
[].foo # => nil
I want them all to return 'bar'
Am aware that you can do this already:
class Object
def foo
'bar'
end
end
But I specifically want to initialize a state variable. Also note that this doesn't work.
class String
alias_method :old_init, :initialize
def initialize(*args)
super
old_init(*args)
end
end
class Object
attr_reader :foo
def initialize
@foo = 'bar'
super
end
end
''.foo # => nil
Nor does this:
class String
attr_reader :foo
def initialize
@foo = 'bar'
end
end
''.instance_variables # => []
I'm beginning to think that this isn't actually possible.