Variations in Variables: JS v Ruby
Here, let me hold that for you
X = X
# Ruby variables: x = "Jan rocks" y = true z = 100 # or x, y, z = "Jan rocks", true, 100
Also in both languages, variables can have either global or local scope, and are accessed simply by calling the variable name.
# Ruby $global = "Anyone can access me!" # this is a global variable def foo local = "Only things inside .foo can access me!" # this is a local variable puts local end puts global
Those variable attributes are about the only things the languages have in common. Let's now look at what they do differently.
JS != R
Ruby actually has multiple explicit variable types with differing scopes and a different syntax for each.
CONSTANT # accessible by all, doesn't change AT ALL in program $global_variable # accessible by all @@class_variable # shared by all instances of a class @instance_variable # shared within one instance of a class local_variable # shared only within a method
Is a Constant still a variable when it is not variable? /deepthoughts
var, whereas Ruby variables just get right to it.
One last difference is actually debatable: Ruby variables can simply be separated with a new line, whereas the JS variables all end with a semicolon. The debatable part is that JS doesn't always require the semicolon, and some versions will let you get away with leaving it off (or forgetting, if you're in the habit of writing Ruby). However, at least today that practice is not encouraged and most JS writers prefer that you go ahead and use the semicolon regardless.
So there you have it! Hopefully that was helpful to review variables in JS and Ruby and how they compare.