Skip to content

Instantly share code, notes, and snippets.

@prodis
Created Jul 18, 2011
Embed
What would you like to do?
Ruby Fundamental - Blocos em Ruby (parte II)
x = 10
5.times do |x|
puts "x inside the block: #{x}"
end
puts "x outside the block: #{x}"
x inside the block: 0
x inside the block: 1
x inside the block: 2
x inside the block: 3
x inside the block: 4
x outside the block: 10
x = 10
5.times do |y|
x = y
puts "x inside the block: #{x}"
end
puts "x outside the block: #{x}"
x inside the block: 0
x inside the block: 1
x inside the block: 2
x inside the block: 3
x inside the block: 4
x outside the block: 4
x = 1200
puts "Before the loop, x = #{x}"
3.times do|y; x|
puts "Looping #{y}"
x = y
end
puts "After the loop, x = #{x}"
Before the loop, x = 1200
Looping 0
Looping 1
Looping 2
After the loop, x = 1200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment