Local variables hold their value during a particular section of code. Local refers therefore to their 'scope' this can be seen through the value of the variable x below:
ruby-1.9.2-p0 > def support_united
ruby-1.9.2-p0 ?> x = "I Support United"
ruby-1.9.2-p0 ?> puts x
ruby-1.9.2-p0 ?> end
ruby-1.9.2-p0 > def support_city
ruby-1.9.2-p0 ?> x = "I Support City"
ruby-1.9.2-p0 ?> puts x
ruby-1.9.2-p0 ?> support_united
ruby-1.9.2-p0 ?> puts "I'm confused now, then who does x support? #{x}"
ruby-1.9.2-p0 ?> end
ruby-1.9.2-p0 > support_city
I Support City
I Support United
I'm confused now, then who does x support? I Support City
ruby-1.9.2-p0 > puts x
woo
Local variables exist within the scope of the method, beginning with def and ending with end
Local variables exist in one of 2 ways
- assignment
x = object - Entry in the methods argument list as shown below
ruby-1.9.2-p0 > def support(team)
ruby-1.9.2-p0 ?> x = "I support #{team}"
ruby-1.9.2-p0 ?> puts x
ruby-1.9.2-p0 ?> end
=> nil
ruby-1.9.2-p0 > support("Rovers")
I support Rovers
Putting the string in as an arg is weird and possibly wrong...
Variables do not hold values as such, they hold a reference to an object, which is why the below happens:
ruby-1.9.2-p0 > myteam = "United"
ruby-1.9.2-p0 > yourteam = myteam
ruby-1.9.2-p0 > yourteam.replace("Borussia Monchengladbach")
ruby-1.9.2-p0 > puts myteam
Borussia Monchengladbach
ruby-1.9.2-p0 > puts yourteam
Borussia Monchengladbach
This shows why that happens:
ruby-1.9.2-p0 > myteam.object_id
=> 2151824460
ruby-1.9.2-p0 > yourteam.object_id
=> 2151824460
and what it can mean:
ruby-1.9.2-p0 > myteam.reverse!
=> "hcabdalgnehcnoM aissuroB"
ruby-1.9.2-p0 > puts yourteam
hcabdalgnehcnoM aissuroB
So how do I reassign
Glad you asked, simply use the = so
ruby-1.9.2-p0 > myteam = "United"
ruby-1.9.2-p0 > yourteam = myteam
ruby-1.9.2-p0 > yourteam = "Aston Villa"
ruby-1.9.2-p0 > puts myteam
United
ruby-1.9.2-p0 > puts yourteam
Aston Villa
To see that in action see below where object.idis output at eachstage
ruby-1.9.2-p0 > myteam = "United"
ruby-1.9.2-p0 > yourteam = myteam
ruby-1.9.2-p0 > puts "Your Team is #{yourteam} ID:#{yourteam.object_id} and My Team is #{myteam} ID:#{myteam.object_id}"
Your Team is United ID:2152670500 and My Team is United ID:2152670500
ruby-1.9.2-p0 > yourteam = "Aston Villa"
ruby-1.9.2-p0 > puts "Your Team is #{yourteam} ID:#{yourteam.object_id} and My Team is #{myteam} ID:#{myteam.object_id}"
Your Team is Aston Villa ID:2152634560 and My Team is United ID:2152670500
Not so fast, its probably a method call, in fact that is the first thing that Ruby will assume it is. First thing after it has looked to see if it can recognise it as a reserved keyword that is. there are 3 options Ruby has when it looks at a word it does not recognise
- does it have an
=to the right of it? then its a variable - its a method call
- fuck off, i don't recognise you e.g.
ruby-1.9.2-p0 > b
NameError: undefined local variable or method `b' for main:Object
from (irb):155
from /Users/toast/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
-
Global:
$foo- not widely used, various reasons, not in fitting with OO, can be difficult to debug -
Class:
@@foo -
Instance: `@foo'
-
Local:
foo
the above variables (local) exist only in the method or code construct that they exist in. Also they do not exist with a value of nil be fore you create them. Neither does a class variable
Every instance variable belongs to whatever object is self at that point in the program.
To think about self, think about 'I' being used in a story told from multiple first person accounts.
myteam = 'foo'
yourteam = 'bar'
list1 = [myteam, yourteam]
list2 = list1.clone
list1 << 'baz'
list1[0] << 'd'
puts list1.inspect
puts list2.inspect