diff options
author | Adam Bard <github@adambard.com> | 2013-08-31 23:10:49 -0700 |
---|---|---|
committer | Adam Bard <github@adambard.com> | 2013-08-31 23:10:49 -0700 |
commit | 13566b6ef6924981054cf38ff6501689dc9ff2e9 (patch) | |
tree | e2a874f94d861f4379fcd185c7ed6bafea7322a6 | |
parent | 63c62eff87064c3d76055d98393032fbd66436db (diff) | |
parent | e13b5111d569903c6620d448f0703707ed1dc8e8 (diff) |
Merge pull request #288 from MarcosX/adding_variables_scope_info
Adding explanation about variables scope and naming convention in Ruby
-rw-r--r-- | ruby.html.markdown | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/ruby.html.markdown b/ruby.html.markdown index 3a233d98..80682682 100644 --- a/ruby.html.markdown +++ b/ruby.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Luke Holder", "http://twitter.com/lukeholder"] - ["Tristan Hume", "http://thume.ca/"] - ["Nick LaMuro", "https://github.com/NickLaMuro"] + - ["Marcos Brizeno", "http://www.about.me/marcosbrizeno"] --- ```ruby @@ -339,6 +340,23 @@ dwight.name #=> "Dwight K. Schrute" # Call the class method Human.say("Hi") #=> "Hi" +# Variable's scopes are defined by the way we name them. +# Variables that start with $ have global scope +$var = "I'm a global var" +defined? $var #=> "global-variable" + +# Variables that start with @ have instance scope +@var = "I'm an instance var" +defined? @var #=> "instance-variable" + +# Variables that start with @@ have class scope +@@var = "I'm a class var" +defined? @@var #=> "class variable" + +# Variables that start with a capital letter are constants +Var = "I'm a constant" +defined? Var #=> "constant" + # Class also is object in ruby. So class can have instance variables. # Class variable is shared among the class and all of its descendants. |