Skip to content

Instantly share code, notes, and snippets.

# Each of the following classes visit the objects, doing something different
# each time.
class CarElementPrintVisitor
def visit(subject)
puts "Visiting: %s" % subject.class.to_s
end
end
class CarElementDoVisitor
def visit(subject)
class Car < CarElement
def initialize
# Here we build an array of objects that will be visited. This can be done
# after initialize as well; it just needs to be built before calling 'accept'.
@elements = []
@elements << Wheel.new("front left")
@elements << Wheel.new("front right")
@elements << Wheel.new("back left")
@elements << Wheel.new("back right")
@elements << Body.new
class CarElement
# force subclasses to override the accept method
def accept(visitor)
raise NotImpelementedError.new
end
end
class Wheel < CarElement
include Visitable # now we have the 'accept' method
module Visitable
# Accept a visitor object. This will be visited, meaning its 'visit' method
# will be invoked
def accept(visitor)
visitor.visit(self)
end
end
@anewusername1
anewusername1 / body.rb
Created January 18, 2012 17:08 — forked from adomokos/visitor_pattern_example.rb
The Visitor Pattern implementation in Ruby from the Wikipedia example
class Body < CarElement
include Visitable # now we have the 'accept' method
end
@anewusername1
anewusername1 / logstash.conf
Created January 13, 2012 20:04
logstash/graylog2 setup
input {
gelf { type => 'gelf' }
}
output {
mongodb {
host => '127.0.0.1'
database => 'rails_logs'
collection => "%{facility}"
}
}
@anewusername1
anewusername1 / bad_spec.rb
Created December 2, 2011 15:42
bad rspec test
it "should allow only strings that include the letter 'a'" do
StringAllower.allowed?('a bed').should == true
StringAllower.allowed?('my bed').should == false
StringAllower.allowed?('single bed').should == false
StringAllower.allowed?('a single bed').should == true
StringAllower.allowed?('some car').should == true
StringAllower.allowed?('some motorcycle').should == false
....
end
@anewusername1
anewusername1 / cleaner_if.rb
Created November 26, 2011 23:12
2011-11-26 post (narshlob)
authorized_human = (is_human == true && is_authorized == true)
invalid_or_errd = (invalid_data == true || error_occurred == true)
if(response && authorized_human && !invalid_or_errd
# do something insane
end
@anewusername1
anewusername1 / gist:1364634
Created November 14, 2011 18:07
fibonacci sequence 0(n)
# the eight element in the fibonacci sequence is 21
n = 8
five_square = Math.sqrt(5)
(((1 + five_square)**n) - ((1 - five_square)**n)) / (2**n * five_square)
@anewusername1
anewusername1 / .alias
Created October 6, 2011 16:17
my gitconfig
alias gb='git branch'
alias gc='git commit -v'
alias gd='git diff'
alias gg='git grep -n'
alias gl='git pull'
alias gm='git merge'
alias gp='git push'
alias gr='git reset'
alias gap='git add -p'
alias gca='git commit -a -v'