Skip to content

Instantly share code, notes, and snippets.

View danbernier's full-sized avatar

Daniel Bernier danbernier

View GitHub Profile
# Turn a PEG on its head! Generate some compliments.
class Complimenter
def compliments
phrases
end
def phrases
phrase + ". " + maybe { phrases }
end
def missing(message)
if message.is_a? Symbol
puts "The environment is missing #{message.to_s.upcase}"
else
puts message
end
end
desc "Check the environment for proper setup"
task :checkenv do
class HasDependents
def initialize(dependent_class=Kid)
@dependent = dependent_class.new
@dependent.parent = self
end
end
class Kid
attr_writer :parent
end
@danbernier
danbernier / gist:5395929
Created April 16, 2013 13:34
Getting back undefined
(function(undefined) {
// In here, undefined is back to normal,
// because we passed the function no params!
})();
@danbernier
danbernier / gist:1579738
Created January 8, 2012 21:20
Ruby lets you add instance variables at runtime
ruby-1.9.2-p290 :001 > class Foo
ruby-1.9.2-p290 :002?> def bonk
ruby-1.9.2-p290 :003?> @bonk = nil
ruby-1.9.2-p290 :004?> end
ruby-1.9.2-p290 :005?> end
=> nil
ruby-1.9.2-p290 :006 > f = Foo.new
=> #<Foo:0x007ff15c1e65b8>
ruby-1.9.2-p290 :007 > f.bonk
=> nil
@danbernier
danbernier / gist:1240775
Created September 25, 2011 16:16
One way I could DRY up hunt_for_bugs and fix_bugs
def hunt_for_bugs(score, location)
bug_work(hidden_bugs(location), :hide_points).size
end
def fix_bugs(score, location)
just_fixed = bug_work(found_bugs(location), :hit_points)
@bug_list.reject! { |bug| bug.dead? }
just_fixed.size
end
@danbernier
danbernier / gist:1231073
Created September 21, 2011 02:38
Markdown insists my String is an Array
# @paragraphs is an Array of Strings - various puts'es confirm this.
@paragraphs.each do |paragraph|
renderer = HH::LessonParaRenderer.new(container)
markdown = Redcarpet::Markdown.new(renderer)
p paragraph # Just checking - it's a String!
# raises: <TypeError: wrong argument type Array (expected String)>
markdown.render(paragraph)
end
module RSlow
class HtmlResource < ParsableResource
attr_accessor :doc, :scripts, :stylesheets, :images
def initialize(url)
super
@doc = Nokogiri::HTML(@contents)
@scripts = fetch_script_resources(".//script", 'src', JsResource)
@stylesheets = fetch_stylesheet_resources(".//link[@rel]", 'href', CssResource)
@images = fetch_image_resources(".//img", 'src', Resource)
module RSlow
class HtmlResource < ParsableResource
attr_accessor :doc, :scripts, :stylesheets, :images
def initialize(url)
super
@doc = Nokogiri::HTML(@contents)
@scripts = fetch_script_resources(".//script", 'src', JsResource)
@stylesheets = fetch_stylesheet_resources(".//link[@rel]", 'href', CssResource)
@images = fetch_image_resources(".//img", 'src', Resource)
stop_words_arg = case stop_words_arg
when :guess then guess_language(string)
when Symbol then stop_words(stop_words_arg)
when Java::CueLangStop::StopWords then stop_words_arg
when nil then nil # is this one really necessary? it documents, but it seems overkill
end