Forum: Ruby Novice: Understanding Constants - Definition and Redefinition

Posted by Steve Tu (stevet)
on 2012-11-30 16:30
Looking at the code from
here:http://rubylearning.com/satishtalim/ruby_constants.html

I copied the Const class into my test module, and bolted it inside a
method. This then caused me a smallish problem, as I then received an
error when trying to set the constants from within a method.

After googling a bit, I found that I could set the constants from within
a method by using the 'class.const_set' method - as:

 class Const
    def get_const
      CONST
    end
    CONST = OUTER_CONST + 1
 end


def constants_test
    STDOUT.puts "Constants Test (Const Is) : "+defined? Const
    STDOUT.puts "Constants Test (Const.new.get_const):
"+Const.new.get_const.to_s
    STDOUT.puts "Constants Test (Const::CONST): "+Const::CONST.to_s
    STDOUT.puts "Constants Test (::OUTER_CONST): "+::OUTER_CONST.to_s
    # Odd, I can set a constant (or even create a new one) like this
line
    Const.const_set("NEW_CONST", 234)
    # But the following line throws a constant redef error
    #STDOUT.puts "Constants Test : (Const::NEW_CONST = 345
)"+(Const::NEW_CONST = 345).to_s
    STDOUT.puts "Constants Test :
(Const::NEW_CONST)"+Const::NEW_CONST.to_s
 end

What I'm then confused about is really two things:
1) Why is it allowable to in effect modify a class? I can follow
extending a class and adding additional features etc (although I haven't
got that far yet) - but I don't quite grasp the concept of
Const::NEW_CONST = value.

2) I thought everything was in a method anyway - but why is it not
allowed to do something like Const::NEW_CONST = value from within a
method, but then allow Const.const_set("CONSTANT", value)?
Posted by 7stud -- (7stud)
on 2012-11-30 20:09
Steve Tu wrote in post #1087312:
>
> What I'm then confused about is really two things:
> 1) Why is it allowable to in effect modify a class?
>

Because ruby allows you to change things at will.  There are still other
ways to change a class other than the ways you have learned so far.  For
instance a user can enter a name and you can add a method with that name
to a class.  Another example: instance variables are private by default
in
ruby, but a programmer can access them at will.  The idea is that if you
really want to break the rules--rather than by accident--ruby lets you.


> 2) I thought everything was in a method anyway - but why is it not
> allowed to do something like Const::NEW_CONST = value
>

Well based on that logic, then because assignment to variables is
typically done with method names that end in '=', for instance:

class Dog
  def initialize(name)
    @name = name
  end

  def name=(name)
    @name = name
  end

  def name
    @name
  end
end


d = Dog.new "Joe"
puts d.name
d.name = 'Al'
puts d.name

--output:--
Joe
Al

...then you might wonder
whether there is a method named "Const::NEW_CONST=" defined somewhere.

> but then allow Const.const_set("CONSTANT", value)?

That's just one example of many demonstrating how ruby lets you break
the rules if you really want to.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.