views:

108

answers:

2
+1  Q: 

Syntax question

What are these variables?

Class User
  TOTO = 1
  TITI = 2
end

User::TOTO # 1
User::TITI # 2

any links to the doc? Thanks,

+9  A: 

TOTO and TITI are defined as constants with values 1 and 2 respectively within the User class.

You can access these constants from within the the user class itself by just referring them as TOTO and TITI

But if you want to access these constants from outside the user class then you have to use the class name as well which is what you are doing I guess, i.e. User::TOTO and User:TITI

Here is little tutorial on the constants.

nas
+2  A: 

Class should be lowercase, for one.

class User
  TOTO = 1
  TITI = 2
end

User::TOTO # 1
User::TITI # 2

User, TOTO and TITI are all constants, with User referencing a class, and TOTO and TITI both being stored inside that class's namespace and both referencing integers.

In ruby, you have several types of variable, all identified by theit starting character.

  • local variables begin with a lowercase letter, and are only accessible within the scope that they are first defined in.
  • constants begin with an uppercase letter, and are accessible within and through the namespace (class or module) that they are defined in.
  • instance variables begin with a single @ sign, and are accessible whenever self evaluates to the instance that eh instance variable was defined in (normally within instance methods)
  • class variables begin with a @@ sign, and are acessible whenever inside the class that first defined them, or any of its subclasses, or any of their instances.
rampion