SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 414 | Next

Ola Bini

"Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java"

Because Ruby doesn??™t have a compilation step that collects all code and reorganizes
it, this means you can do some interesting things in class definitions that aren??™t
possible in Java.
Naming
Ruby uses naming to determine what kind of variable you??™re referencing. This can be useful
because it gives you a visual cue about what is happening. You can use five different types of
names. What??™s common for all of them is that they spring into being the first time you give a
value to them.
Globals
Any name starting with the dollar sign is a global variable and will have the same value no
matter from where you reference it. The value of a global can be changed, and that change will
be seen in all code running after that.
289
A P P E N D I X A
$foobar = "Hello World"
def print_foobar
print $foobar
end
print_foobar
Constants
All names beginning with a capital letter are constants. A constant belongs to a certain scope,
and constants can be nested inside modules and classes. Class names and module names are
almost always constants, but that doesn??™t mean a constant can??™t contain another value. Typical
class constants are String, Hash, and Array. You can set other constants like this:
Value = 1
VALUE_NO_2 = 2
module Mod
VALUE_3 = 3
end
puts Value
puts VALUE_NO_2
puts Mod::VALUE_3
Notice that you reference a constant inside a module using ::. You can reset constants
to another value, but that causes a warning message to be printed.


Pages:
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426