Skip to content

Instantly share code, notes, and snippets.

View Sihui's full-sized avatar

Sihui Huang Sihui

View GitHub Profile
@Sihui
Sihui / return_a_proc.rb
Last active April 27, 2017 06:05
For [Code Block, Proc, Lambda, and Closure in Ruby]()
# 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
@Sihui
Sihui / construct_proc_lambda.rb
Last active June 4, 2018 21:11
For [Code Block, Proc, Lambda, and Closure in Ruby]() Raw
Proc.new { puts 'I am a proc' }
proc { puts 'I am a proc' }
lambda { puts 'I am a lambda' }
-> { puts 'I am a lambda' }
@Sihui
Sihui / construct3.rb
Last active April 27, 2017 05:51
For [Code Block, Proc, Lambda, and Closure in Ruby]()
#Lambda
# 1. lambda followed by a code block
lambda { puts 'Hi' }
lambda do
puts 'Hi'
end
#2. -> (stabby lambda)
-> { puts 'Hi' }
-> do
puts 'Hi'
@Sihui
Sihui / construct2.rb
Last active April 27, 2017 05:49
For [Code Block, Proc, Lambda, and Closure in Ruby]()
#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'
@Sihui
Sihui / construct1.rb
Last active April 27, 2017 05:49
For [Code Block, Proc, Lambda, and Closure in Ruby]()
#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}
@Sihui
Sihui / construct4.rb
Created April 27, 2017 05:53
For [Code Block, Proc, Lambda, and Closure in Ruby]() Raw
# 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 }
@Sihui
Sihui / go_to_orders.rb
Last active July 14, 2017 15:17
Design Pattern: Template Method and Chipotle
class GoToOrderSihui
def vessel
Bowl
end
def meat
Steak
end
def toppings
@Sihui
Sihui / diet_order_tmp.rb
Last active September 13, 2017 01:57
Design Pattern: Template Method and Chipotle Raw
class DietOrder
def vessel
raise 'What is your choice?'
end
def meat
raise 'What is your choice?'
end
def toppings
@Sihui
Sihui / diet_orders.rb
Last active July 14, 2017 15:16
Design Pattern: Template Method and Chipotle Raw
class DietOrderSihui
def vessel
Bowl
end
def meat
Steak
end
def toppings
@Sihui
Sihui / ben_diet_order.rb
Last active July 14, 2017 04:41
Design Pattern: Template Method and Chipotle
class BenDietOrder < DietOrder
def vessel
Tacos
end
def meat
Carnitas
end
end