Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Last active February 1, 2021 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithrbennett/777965ca80949f707c968e59740e6d44 to your computer and use it in GitHub Desktop.
Save keithrbennett/777965ca80949f707c968e59740e6d44 to your computer and use it in GitHub Desktop.
Illustrates differences in behavior between (until, for) and (loop, map, upto).
#!/usr/bin/env ruby
# --------------------------------------------------------------------------------
# test_iteration_constructs.rb
#
# Illustrates differences in behavior of some iteration constructs.
# @keithrbennett, 2021-02-01
#
# Output is:
#
# Until:
# zzzzzzzzzzzzzzzzzzzzzzzzzz until Can access local variable outside loop.
#
# Loop:
# abcdefghijklmnopqrstuvwxyz loop CANNOT access local variable outside loop.
#
# For...in:
# zzzzzzzzzzzzzzzzzzzzzzzzzz for...in Can access local variable outside loop.
#
# Map:
# abcdefghijklmnopqrstuvwxyz map CANNOT access local variable outside loop.
#
# Upto:
# abcdefghijklmnopqrstuvwxyz upto CANNOT access local variable outside loop.
# --------------------------------------------------------------------------------
# zzzzzzzzzzzzzzzzzzzzzzzzzz until Can access local variable outside loop.
def do_until
puts "\nUntil:"
lambdas = []
vals = ('a'..'z').to_a
until vals.empty?
val = vals.shift()
lambdas << -> { print val }
end
lambdas.each(&:call)
test_can_access_var_outside_loop('until', %Q{ n = 0; until n > 2; x = 4; n += 1; end ; x += 1})
end
# abcdefghijklmnopqrstuvwxyz loop CANNOT access local variable outside loop.
def do_loop
puts "\nLoop:"
lambdas = []
vals = ('a'..'z').to_a
loop do
break if vals.empty?
val = vals.shift()
lambdas << -> { print val }
end
lambdas.each(&:call)
test_can_access_var_outside_loop('loop', %Q{ n = 0; loop { x = n; n+= 1; break if n > 4 }; x += 1})
end
# zzzzzzzzzzzzzzzzzzzzzzzzzz for...in Can access local variable outside loop.
def do_for_in
puts "\nFor...in:"
lambdas = []
vals = ('a'..'z').to_a
for val in vals do
lambdas << -> { print val }
end
lambdas.each(&:call)
test_can_access_var_outside_loop('for...in', %Q{for s in %w{a b} do; x = 4; x += 1; end})
end
# abcdefghijklmnopqrstuvwxyz map CANNOT access local variable outside loop.
def do_map
puts "\nMap:"
lambdas = ('a'..'z').map { |val| -> { print val } }
lambdas.each(&:call)
test_can_access_var_outside_loop('map', %Q{%w{a b}.map { |_| x = 4 }; x += 1})
end
# abcdefghijklmnopqrstuvwxyz upto CANNOT access local variable outside loop.
def do_upto
puts "\nUpto:"
lambdas = []
'a'.upto('z') { |val| lambdas << -> { print val } }
lambdas.each(&:call)
test_can_access_var_outside_loop('upto', %Q{'a'.upto('z') { |_| x = 4 }; x += 1})
end
def test_can_access_var_outside_loop(label, code_string)
begin
eval(code_string)
result = 'Can'
rescue
result = 'CANNOT'
end
puts(" #{label} #{result} access local variable outside loop.")
end
do_until
do_loop
do_for_in
do_map
do_upto
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment