View named_dynamic_methods.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# It's common, while creating a bunch of very similar methods, to create | |
# a loop to define them all at once. (Very simple) example follows: | |
week_days = %w[sunday monday tuesday wednesday thursday friday saturday] | |
week_days.each do |day| | |
define_method("events_on_#{day}") do | |
events.select { |event| event.day == day } | |
end | |
end |
View gist:1290010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Inject | |
class Injector | |
def self.inject(*args, &block) | |
options = args.last.kind_of?(Hash) ? args.pop : {} | |
target = options[:receiver] || block.binding.eval("self") | |
modules = args | |
modules.each { |mod| mod.before_inject(target) if mod.respond_to?(:before_inject)} | |
return_value = new(modules).inject_into(target, &block) |
View gist:1311185
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Monad | |
data Tree a = EmptyTree | Node a (Tree a) (Tree a) | |
(<<) EmptyTree value = Node value EmptyTree EmptyTree | |
(<<) (Node value left right) newValue = if newValue < value | |
then Node value (left << newValue) right | |
else Node value left (right << newValue) | |
toArray EmptyTree = [] |
View xmonad-unity.desktop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /usr/share/xsessions/xmonad-unity.desktop | |
[Desktop Entry] | |
Encoding=UTF-8 | |
Name=XMonad/Unity | |
Comment=Xmonad+Unity | |
Exec=gnome-session --session=xmonad_unity | |
Icon=xmonad.png | |
Type=Application |
View user.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < Struct.new(:username, :private) | |
alias_method :private?, :private | |
def follow | |
follow_strategy.call | |
end | |
private | |
def follow_strategy |
View module.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AttributeAccessor < Module | |
def initialize(name) | |
@name = name | |
module_eval { define_accessors } | |
end | |
private | |
def define_accessors | |
ivar = "@#{@name}" |
View fiber-example.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Depends on gems eventmachine, em-http-request, | |
# and em-resolv-replace (for # async DNS) | |
require 'eventmachine' | |
require 'em-http' | |
require 'fiber' | |
require 'em-resolv-replace' | |
def make_request_to(url) | |
connection = EventMachine::HttpRequest.new(url) |
View nth_child.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'nokogiri' | |
require 'rspec' | |
describe "nth-child" do | |
let(:doc) do | |
Nokogiri::HTML(<<-HTML) | |
<html> | |
<body> | |
<p id="first"></p> | |
<p id="second"></p> |
View patch.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The filter_parameter_logging method, used to filter request parameters | |
# (such as passwords) from the log, defines a protected method called | |
# filter_parameter when called. Its existence is later tested using | |
# respond_to?, without the include_private parameter. Due to the respond_to? | |
# behavior change, the method existence is never detected, and parameter | |
# filtering stops working. | |
require 'action_controller' | |
module ParameterFilterPatch | |
def respond_to?(method, include_private = false) |
View results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
running 2 tests | |
test with_channels ... bench: 876410 ns/iter (+/- 655463) | |
test with_semaphores ... bench: 513476 ns/iter (+/- 711503) | |
test result: ok. 0 passed; 0 failed; 0 ignored; 2 measured |
OlderNewer