Question: ```
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.

Answer: 
If the thing you're trying to return (here, `'bar'`) requires much more setup cost than a small String, you could use the memoized version of [HermanD's answer](https://stackoverflow.com/questions/914814/in-ruby-how-can-i-initialize-instance-variables-in-new-objects-of-core-classes-c/914838#914838):

```
class Object
  def foo
    @foo ||= begin
      'bar' # something far more complicated than just "'bar'"
    end
  end
end

```

You could even use the memoization for cheap stuff like Strings if you *really* need the instance variable to be set (for some reason that I can't quite think of at them moment):

```
class Object
  def foo
    @foo ||= 'bar'
  end
end

```
It's not an instance variable, but will meet the requirements of your example

```
class Object
  def foo
    'bar'
  end
end

```