meso_2600 meso_2600 - 1 year ago 92
Ruby Question

How make a variable public final in Ruby

I would like to create a class that during initialization of an Object of this class would assign provided value to one of the variables, in such way it can't be changed.
For example:

person ="Tom") #=> Tom = "Bob"

this should raise an error or: #=> Tom -> still

Answer Source
class Person
  def initialize name
    @name = name
  attr_reader :name

person ="Tom")         #=> Tom
begin = "Bob"
  puts $!.message   # => Undefined method error
end         #=> Tom
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download