arr = [1,2,3,4, 5, 6].map(&lambda{|x| x * x})
puts arr
[1, 4, 9, 16, 25, 36]
arr2 =[1,2,3,4].map do |x| x * x end
puts arr2
[1, 4, 9, 16]
myproc = Proc.new do |x|
x * x
end
#<Proc:0x0000000176e228@<main>:0>
arr3 = [1,2,3,4].map(&myproc)
puts arr3
[1, 4, 9, 16]
arr2.select(&:even?)
[4, 16]
def greet(name, informal=false)
if informal
"hi #{name}"
else
"hello #{name}"
end
end
:greet
greet 'john'
"hello john"
greet('jane', informal=true)
"hi jane"