Skip to content

Instantly share code, notes, and snippets.

View nathankleyn's full-sized avatar
👨‍💻
Creating!

Nathan Kleyn nathankleyn

👨‍💻
Creating!
View GitHub Profile
@nathankleyn
nathankleyn / gist_1_functional_ruby.rb
Created February 6, 2012 11:45
Gist #1 For "Functional Programming Techniques With Ruby: Part I"
x = x + 1
@nathankleyn
nathankleyn / gist_2_functional_ruby.rb
Created February 6, 2012 11:45
Gist #2 For "Functional Programming Techniques With Ruby: Part I"
x - x = 1
∴ 0 = 1 (!?)
@nathankleyn
nathankleyn / gist_7_functional_ruby.rb
Created February 6, 2012 11:47
Gist #7 For "Functional Programming Techniques With Ruby: Part I"
class CssBlock
attr_reader :selector, :properties
def initialize(selector, properties = {})
@selector = selector.dup.freeze
@properties = properties.dup.freeze
end
end
@nathankleyn
nathankleyn / gist_8_functional_ruby.rb
Created February 6, 2012 11:49
Gist #8 For "Functional Programming Techniques With Ruby: Part I"
class CssBlock
# ...
def set(key, value = nil)
new_properties = if key.is_a?(Hash)
key
elsif !value.nil?
{
key => value
@nathankleyn
nathankleyn / gist_5_functional_ruby.rb
Created February 6, 2012 11:46
Gist #5 For "Functional Programming Techniques With Ruby: Part I"
def module_split(module_path, separator = "::")
modules = module_path.split(separator)
modules.length.downto(1).map { |n| modules.first(n).join(separator) }
end
module_split("W::X::Y::Z")
@nathankleyn
nathankleyn / gist_6_functional_ruby.rb
Created February 6, 2012 11:46
Gist #6 For "Functional Programming Techniques With Ruby: Part I"
class CssBlock
# We'll add some methods in here.
end
@nathankleyn
nathankleyn / gist_10_functional_ruby.rb
Created February 6, 2012 11:50
Gist #10 For "Functional Programming Techniques With Ruby: Part I"
CssBlock.new("#some_id .class a").set("color", "#FFF").set({ "color" => "#000", "text-decoration" => "underline"}) # => "#some_id .class a { color: #000; text-decoration: underline }"
@nathankleyn
nathankleyn / gist_3_functional_ruby.rb
Created February 6, 2012 11:45
Gist #3 For "Functional Programming Techniques With Ruby: Part I"
x = 1
y = x + 1
@nathankleyn
nathankleyn / gist_4_functional_ruby.rb
Created February 6, 2012 11:46
Gist #4 For "Functional Programming Techniques With Ruby: Part I"
y = x + 1
∴ x = y - 1
@nathankleyn
nathankleyn / gist_11_functional_ruby.rb
Created February 6, 2012 11:51
Gist #11 For "Functional Programming Techniques With Ruby: Part I"
class CssBlock
attr_reader :selector, :properties
def initialize(selector, properties = {})
@selector = selector.dup.freeze
@properties = properties.dup.freeze
end
def set(key, value = nil)
new_properties = if key.is_a?(Hash)