Class_block
class One
def initialize = ( @name = 'olle' )
end
one = One.new
puts one.name # => NoMethodError
# The class gives no way to access 'name'.
# (unless)
puts one.instance_variable_get('@name')
# adding getter-method to instance
one.class.send(:attr_reader, :name)
puts one.name # => olle
# ---
# You can also gain access to the namespace via a block.
class Two
def initialize(&bl)
@name = 'olle'
instance_exec(&bl)
end
end
Two.new { puts @name } # => olle