Does Ruby actually do anything different with a "constant" as opposed to a "class variable"?

I'm reading through a Ruby book ("Head First Ruby"). I would have preferred a "Ruby for Java Devs" book, but this works.

I just read about how to define "constants" by setting a variable at class level that begins with an uppercase character.

I note the similarity of how you define a constant compared to a "class variable", which looks like an instance variable (with "@"), but at the class level, not in the method.

The question is, what is really happening here? Does Ruby really do something different with a variable set at class level if it begins with an uppercase character, or is this just a "convention" for constants?

I tried a simple experiment of setting both "const" and "CONST" at class level, and then defining a method that tries to set those. The Eclipse Ruby plugin does give me an error when I try to assign to "CONST" in the method, saying "dynamic constant assignment".

That leads to another rambling question: What's the difference between a "class variable" defined with "@" and one without it?

Perhaps a reference will clarify.

https://en.m.wikibooks.org/wiki/Ruby_Programming/Syntax/Variables_and_Constants

This seemed pretty clear, but if you have additional questions do not
hesitate to reach out.

Oh, capitalization of constants is not convention, it is part of the
language. It ous covered in the link.

Brad

···

On Apr 21, 2016 3:06 PM, "David M. Karr" <davidmichaelkarr@gmail.com> wrote:

I'm reading through a Ruby book ("Head First Ruby"). I would have
preferred a "Ruby for Java Devs" book, but this works.

I just read about how to define "constants" by setting a variable at class
level that begins with an uppercase character.

I note the similarity of how you define a constant compared to a "class
variable", which looks like an instance variable (with "@"), but at the
class level, not in the method.

The question is, what is really happening here? Does Ruby really do
something different with a variable set at class level if it begins with an
uppercase character, or is this just a "convention" for constants?

I tried a simple experiment of setting both "const" and "CONST" at class
level, and then defining a method that tries to set those. The Eclipse Ruby
plugin does give me an error when I try to assign to "CONST" in the method,
saying "dynamic constant assignment".

That leads to another rambling question: What's the difference between a
"class variable" defined with "@" and one without it?

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

I'm reading through a Ruby book ("Head First Ruby"). I would have preferred a "Ruby for Java Devs" book, but this works.

I just read about how to define "constants" by setting a variable at class level that begins with an uppercase character.

I note the similarity of how you define a constant compared to a "class variable", which looks like an instance variable (with "@"), but at the class level, not in the method.

The question is, what is really happening here? Does Ruby really do something different with a variable set at class level if it begins with an uppercase character, or is this just a "convention" for constants?

I tried a simple experiment of setting both "const" and "CONST" at class level, and then defining a method that tries to set those. The Eclipse Ruby plugin does give me an error when I try to assign to "CONST" in the method, saying "dynamic constant assignment".

That leads to another rambling question: What's the difference between a "class variable" defined with "@" and one without it?

A variable starting with an `@` is called an instance variable -- if defined in a class body, it will be visible from singleton methods within that class.
There are also class variables, which start with `@@` and behave a little differently. (try google)

A variable starting with a lowercase letter is a **regular variable**, it is not tied to a class or anything.

A constant (starting with uppercase letter) can:
a) not be changed
b) be referenced under the **namespace** of the class / module where it is defined, such as:

class Foo
   CONST = 123
end

CONST #=> raises undefined sth
Foo::CONST #=> returns 123

Hope this helps.
cheers,
recmm

···

On 04/21/2016 08:06 PM, David M. Karr wrote:

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

That document says that every class is a constant, which is not true. Also,
the explanation of the resolution of constants is incomplete, does not even
mention ancestors!

This Rails guide covers constants with more depth:

http://guides.rubyonrails.org/autoloading_and_reloading_constants.html#constants-refresher

@David Constants are normally pretty trivial in most programming languages,
but a big topic in Ruby.