This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Returning a proc from a method | |
def fancy_proc_maker | |
proc { puts 'Call me fancy' } | |
end | |
fancy_proc = fancy_proc_maker | |
fancy_proc.call | |
# The above code will print: | |
# Called me fancy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Proc.new { puts 'I am a proc' } | |
proc { puts 'I am a proc' } | |
lambda { puts 'I am a lambda' } | |
-> { puts 'I am a lambda' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Lambda | |
# 1. lambda followed by a code block | |
lambda { puts 'Hi' } | |
lambda do | |
puts 'Hi' | |
end | |
#2. -> (stabby lambda) | |
-> { puts 'Hi' } | |
-> do | |
puts 'Hi' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Proc | |
#1. Proc.new followed by a code block | |
Proc.new { puts 'Hi' } | |
Proc.new do | |
puts 'Hi' | |
end | |
#2. proc followed by a code block | |
proc { puts 'Hi' } | |
proc do | |
puts 'Hi' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Code Block | |
#1 {} Mostly used as a one-liner | |
{ puts 'Hi' } | |
#2. do end | |
do | |
puts 'Hi' | |
end | |
#arguments are | |
# wrapped between || | |
[1, 2].map {|num| num * 2} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Only -> | |
# takes argument outside of {} | |
->(arg) { puts arg } | |
# The rest | |
# takes argument in between || | |
{ |arg| puts arg } | |
proc do |arg| | |
puts arg | |
end | |
lambda { |arg| puts arg } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GoToOrderSihui | |
def vessel | |
Bowl | |
end | |
def meat | |
Steak | |
end | |
def toppings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DietOrder | |
def vessel | |
raise 'What is your choice?' | |
end | |
def meat | |
raise 'What is your choice?' | |
end | |
def toppings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DietOrderSihui | |
def vessel | |
Bowl | |
end | |
def meat | |
Steak | |
end | |
def toppings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BenDietOrder < DietOrder | |
def vessel | |
Tacos | |
end | |
def meat | |
Carnitas | |
end | |
end |