walking through 3 code snippets
TRANSCRIPT
Walking Through 3 Code Snippets
AKA: Pointers and Scope are important
Code to Run Through
• Tanner’s Array#Each• Oleg’s Default Hash Modification• Marcus’ Blorgotron Closure Scope
Tanner’s Array#Each
fruits = %w{ orange apple strawberry banana mango }
fruits.count.times { |i| fruits[i] += " is delicious" }
fruits.each { |fruit| fruit += " is delicious" }
from http://bitsandbytesandallthingsnice.tumblr.com/post/25137722066/array-each-returns-same-array
Given this array
This works
But this doesn’t
Tanner’s Array#Each
fruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" endfruits.each do |fruit| fruit += " is “delicious”end
Rewritten Slightly…
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" endfruits.each do |fruit| fruit += " is delicious" }end
Track Variables Here
(AKA The Global Environment)
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" endfruits.each do |fruit| fruit += " is delicious" }end
Variables Names Working Memory
Tanner’s Array#Eachfruits = %w{ orange apple mango }
orangeapplemango
Tanner’s Array#Eachfruits = %w{ orange apple mango }
orangeapplemango
[] 0 1 2
Tanner’s Array#Eachfruits = %w{ orange apple mango }
orangeapplemango
[] 0 1 2
fruits
Tanner’s Array#Eachfruits = %w{ orange apple mango }
orangeapplemango
[] 0 1 2
fruits
Sidenote: Order of OperationsIn Math• Parentheses First• Multiplication / Division• Addition / Subtraction
(5 + 3) + 12 * 4 / 2
8 + 48 / 2
8 + 24
32
Tanner’s Array#Eachfruits = %w{ orange apple mango }
orangeapplemango
[] 0 1 2
fruits
Sidenote: Order of OperationsIn Programming• Parentheses (arguments) first• Left to right• Assignment (=) is last
x = 3.add(5 + 2)
x = 3.add(7)
x = 10
Tanner’s Array#Eachfruits = %w{ orange apple mango }
orangeapplemango
[] 0 1 2
fruits
Sidenote: Order of OperationsIn Programming• Parentheses (arguments) first• Exceptions• Logic
• Left to right• Assignment (=) is last
Tanner’s Array#Eachfruits = %w{ orange apple mango }
orangeapplemango
[] 0 1 2
fruits
Sidenote: Order of OperationsIn Programming• Parentheses (arguments) first• Exceptions• Logic
• Left to right• Assignment (=) is last
puts (“something true” || something_illegal)
Tanner’s Array#Eachfruits = %w{ orange apple mango }
orangeapplemango
[] 0 1 2
fruits
Sidenote: Order of OperationsIn Programming• Parentheses (arguments) first• Exceptions• Logic• Function / Block Definitions
• Left to right• Assignment (=) is last
Tanner’s Array#Each
fruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" end
Snippet 1: The one that works
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" end
orangeapplemango
[] 0 1 2
fruits
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.count.times do |i| fruits[i] += " is delicious" end
orangeapplemango
[] 0 1 2
fruits
Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] += " is delicious" end
orangeapplemango
[] 0 1 2
fruits
Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
0
Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
0
i
Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
i
orange
0
Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
i
orange “ is delicious"
0
Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
i
orange “ is delicious"
0
orange is delicious
Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious"end
orangeapplemango
[] 0 1 2
fruits
i
orange is delicious
0
Tanner’s Array#Eachfruits = %w{ orange apple mango }3.times do |i| fruits[i] = fruits[i] + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
i orange is delicious
i apple is delicious
i mango is delicious
0
1
2
Tanner’s Array#EachSnippet 2: The one that doesn’t work
fruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
orange
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
orange
fruit
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious"
end
orangeapplemango
[] 0 1 2
fruits
orange
fruit
orange is delicious
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
orange
fruit
orange is delicious
Tanner’s Array#Eachfruits = %w{ orange apple mango }fruits.each do |fruit| fruit = fruit + " is delicious" end
orangeapplemango
[] 0 1 2
fruits
fruit orange is delicious
fruit apple is delicious
fruit mango is delicious
Tanner’s Array#Each
Let’s compare the two ways
Tanner’s Array#Each
This example does the work, but the work is unreachablefruits.each { |fruit| fruit += fruit " is delicious" }
orangeapplemango
[] 0 1 2
fruits
This example worksfruits.count.times {|i| fruits[i] += " is delicious" }
[] 0 1 2
fruits orange is delicious
apple is delicious
mango is delicious
fruit orange is delicious
fruit apple is delicious
fruit mango is delicious
Unreacha
ble!
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]Hsh[“other_key”]
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
stuff
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
{}else
stuff
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
{}else
stuffhsh
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
{}else
stuffhsh
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
{}else
stuffhsh
stuff
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
{}else
stuffhsh
stuff
<<
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
{}else
stuffhsh stuff
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
{}else
stuffhsh stuff
something else
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
{}keyelse
stuffhsh stuff
something else
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
{}keyelse
stuffhsh stuff
something else
Oleg’s Default Hash Modificationhsh = Hash.new(‘stuff’)hsh[“key”]hsh[“key”] << “stuff”hsh[“key”]hsh[“key”] = “something else”hsh[“key”]hsh[“other_key”]
{}keyelse
stuffhsh stuff
something else
Marcus’ Blorgotron Closure Scopevar Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
Marcus’ Blorgotron Closure Scopevar Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
Marcus’ Blorgotron Closure Scopevar Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
Marcus’ Blorgotron Closure Scope
0.53
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
Marcus’ Blorgotron Closure Scope
0.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
Marcus’ Blorgotron Closure Scope
0.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
function(){return secret;}function(){secret++;}
Marcus’ Blorgotron Closure Scope
0.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
Marcus’ Blorgotron Closure Scope
0.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
blorg
Marcus’ Blorgotron Closure Scope
0.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
blorg
Marcus’ Blorgotron Closure Scope
0.53secret
var Blorgotron = function() { secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
blorg
blorg.get()
blorg[“get”]()
{get: function(){ return secret;}, inc: function(){secret++;} } }[“get”]()
function(){ return secret;}()
Evaluating blorg.get()
Marcus’ Blorgotron Closure Scope
0.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
blorg
Marcus’ Blorgotron Closure Scope
1.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
blorg
Marcus’ Blorgotron Closure Scope
1.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
blorg
Marcus’ Blorgotron Closure Scope
1.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
blorg 0.24secret{}getinc
function(){return secret;}function(){secret++;}
Marcus’ Blorgotron Closure Scope
1.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
blorg 0.24secret{}getinc
function(){return secret;}function(){secret++;}
blorg2
Marcus’ Blorgotron Closure Scope
1.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
blorg 1.24secret{}getinc
function(){return secret;}function(){secret++;}
blorg2
Marcus’ Blorgotron Closure Scope
1.53secret
var Blorgotron = function() { var secret = Math.random(); return { get: function(){ return secret;}, inc: function(){secret++;} } }
blorg = Blorgotron()blorg.get()blorg.inc()blorg.get()
blorg2 = Blorgotron()blorg2.inc()
{}getinc
function(){return secret;}function(){secret++;}
blorg 1.24secret{}getinc
function(){return secret;}function(){secret++;}
blorg2