Skip to content

Instantly share code, notes, and snippets.

Avatar
😀

Jim Gay saturnflyer

😀
View GitHub Profile
@saturnflyer
saturnflyer / the_world_of_foos.rb
Last active Jul 16, 2019 — forked from zdennis/the_world_of_foos.rb
Given the below hierarchy how many ways can you think of to wrap the `Parent#foo` implementation so that `Grandchild1` and `Grandchild2` can have their own unique logic that executes after all of the mixin `foo`(s) but before the `Parent#foo` executes?
View the_world_of_foos.rb
module N
def foo
puts "N: foo"
super
end
end
module O
def foo
puts "O: foo"
@saturnflyer
saturnflyer / dynamic_initialize.rb
Created Jul 10, 2013
Dynamically defining the initialize method and setting the arity.
View dynamic_initialize.rb
class Something
def self.init(*init_args)
# this works but...
define_method(:initialize){|*args|
# arity is -1
}
class_eval %Q<
def initialize(#{*init_args})
@saturnflyer
saturnflyer / json-generator.rb
Last active May 24, 2017
Using casting to apply different behaviors to an object for generating JSON
View json-generator.rb
require 'json'
require 'casting'
module Names
def names
"names here"
end
end
module Secrets
@saturnflyer
saturnflyer / prepended.rb
Last active May 11, 2017 — forked from cmar/prepended.rb
example of prepended class methods
View prepended.rb
class Foo
def self.say
p "hello from Foo"
end
end
module Bar
def say
super
p "hello from Bar"
View modulemethods.rb
module Foo1
def self.bar
"Yolo"
end
end
"Foo1.bar => #{Foo1.bar}" # => "Foo1.bar => Yolo"
module Foo2
def bar # !> previous definition of bar was here
"Yolo"
@saturnflyer
saturnflyer / namespace.rb
Last active Jan 13, 2016
ability to create classes and modules in a namespace
View namespace.rb
module Namespace
class Manager
def initialize(namespace)
@managed = namespace
end
def create_module(name, &block)
mod = Module.new(&block)
@managed.const_set(name, mod)
mod
@saturnflyer
saturnflyer / exception.rb
Created Nov 26, 2013
Exception cause patch for access like a null object
View exception.rb
class Exception
def cause_message
cause && cause.message
end
def cause_backtrace
cause && cause.backtrace
end
end
View gist:7089002
Introduction .....................................................................................................6
What This Is ..................................................................................................................8
What This Is Not ...........................................................................................................8
Lost In Translation...........................................................................................9
Our First Bugs..............................................................................................................10
Object Orientation ........................................................................................12
Where We Are..............................................................................................................12
Where We’ve Been .......................................................................................................20
Where We’re Going ....................
View presenter.rb
require 'delegate'
# Public: This class initializes a presenter object.
# The presenter wraps a given object and a view object and will forward
# method calls to each. By default, any unknown method will be passed
# to the first object.
# Any methods that need to be sent to the view, may be done explicitly.
#
# Examples:
#
# # Inside a controller you create a helper method to access this object
@saturnflyer
saturnflyer / topics.md
Last active Dec 22, 2015
newsletter topics
View topics.md
  1. Ruby Delegate.rb Secrets
  2. Simple presenters to clean up views
  3. Ruby Forwardable in Depth
  4. how it works
  5. SingleForwardable
  6. identifying responsibility leaks with if
  7. enforcing encapsulation
  8. seeing the westward flow and heading east
  9. ActiveSupport::Delegation, the Rails approach to forwarding
  10. The Casting gem and why it's different from SimpleDelegator