loading
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Code Block are like anonymous functions that can't be saved to a variable and must be written in-line as a function argument when calling an iterator of some kind. They are invoked when the iterator yields.
puts "Blocks"
[1, 2, 3].each { |num| puts num }

# It seems that a Code Block cannot be saved to a variable.
# It also seems that I can't pass a function in place of a code block
# However, I can write a function and then map arugments via a code block
puts "Blocks mapping Functions"
def my_func(num)
  puts num
end 
[1, 2, 3].each {|num| my_func(num)}

# Ruby Procs
# I think I misspoke when I said code blocks can't be persisted. It seems that they can via the Proc class
puts "Procs"
my_proc = Proc.new { |num| puts num }
[1, 2, 3].each(&my_proc)

# Lambdas
# Seem to be similar to Procs, but you can safely use the return keyword
puts "Lambdas"
my_lambda = ->(num) { puts num }
[1, 2, 3].each(&my_lambda)
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux]