Skip to content

Instantly share code, notes, and snippets.

View Sihui's full-sized avatar

Sihui Huang Sihui

View GitHub Profile
config.assets.paths += [
Rails.root.join('vendor', 'assets', 'fonts')
]
config.assets.precompile += [
'icons.eot',
'icons.svg',
'icons.ttf',
'icons.woff'
]
# Concrete Strategy
class GrilledChickenStuffing
def cook
stuffing = []
stuffing << 'sliced tomato'
stuffing << 'lettuce'
stuffing << 'grilled chicken breast'
stuffing
end
end
class GrilledChicken
def cook
stuffing = []
stuffing << 'sliced tomato'
stuffing << 'lettuce'
stuffing << 'grilled chicken'
stuffing
end
end
class Burger
attr_reader :top_bun, :bottom_bun
attr_accessor :stuffing
def initialize(stuffing)
@top_bun = TOP_BUN
@stuffing = stuffing
@bottom_bun = BOTTOM_BUN
end
chicken_burger
= Burger.new(GrilledChicken.new)
cheese_burger
= Burger.new(BeefPatty.new)
@Sihui
Sihui / call_proc.rb
Created April 23, 2017 17:19
For [Code Block, Proc, Lambda, and Closure in Ruby]()
times_2 = Proc.new { |num| num * 2 }
times_2.call(3) # returns 6
times_2 = proc { |num| num * 2 }
times_2.call(3) # returns 6
times_2 = lambda { |num| num * 2 }
times_2.call(3) # returns 6
times_2 = -> (num) { num * 2 }
@Sihui
Sihui / yield_block_with_arg.rb
Created April 23, 2017 17:45
For [Code Block, Proc, Lambda, and Closure in Ruby]()
# Example: passing arguments
def ten_plus
puts 'before yield'
yield(10)
puts 'after yield'
end
ten_plus do |num|
puts(num + 100)
end
@Sihui
Sihui / yield_block.rb
Last active April 27, 2017 03:48
For [Code Block, Proc, Lambda, and Closure in Ruby]()
def burger
puts 'top bun'
yield if block_given?
puts 'bottom bun'
end
burger do
puts 'lettuce'
puts 'tomato'
puts 'chicken breast'
@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 / 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'