module A
def foo
puts 'foo from A'
super
end
end
module B
def foo
This file contains hidden or 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
## | |
# Note from Ruby Pocket Reference | |
# | |
# Since constants refer to objects, the contents of the object | |
# to which the constant refers may change without Ruby generating | |
# a warning. Thus, Ruby constants are called *mutable*, because, | |
# although the constant is only expected to refer to a single | |
# object throughout the program, what's contained in that object | |
# may vary. |
This file contains hidden or 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 MyMethods | |
def my_public_method | |
puts "Public" | |
end | |
protected | |
def my_protected_method | |
puts "Protected" | |
end |
This file contains hidden or 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
def subclasses_of(superclass) | |
subclasses = [] | |
ObjectSpace.each_object(Class) do |k| | |
next if !k.ancestors.include?(superclass) || superclass == k || k.to_s.include?('::') || subclasses.include?(k.to_s) | |
subclasses << k.to_s | |
end | |
subclasses | |
end |
This file contains hidden or 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
use Rack::Auth::Basic, 'app' do |usr, pwd| | |
usr == 'wendi' && pwd == '123123' | |
end | |
# => Request Headers | |
# Authorization:Basic d2VuZGk6MTIzMTIz | |
# realm = 'Hello, wrold' | |
# opaque = '1234567890' | |
# use Rack::Auth::Digest::MD5, realm, opaque do |password| |
This file contains hidden or 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/bin/env ruby | |
require 'date' | |
require 'fileutils' | |
app_key = '' | |
DUMP_HOST = 'localhost' | |
DUMP_PORT = '27017' |
This file contains hidden or 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
# catch executes its block. | |
# If a throw is executed, Ruby searches up its stack for a catch block with a tag corresponding to the throw's tag. | |
# If found, that block is terminated, and catch returns the value given to throw. | |
# If throw is not called, the block terminates normally, and the value of catch is the value of the last expression evaluated. | |
# catch expressions may be nested, and the throw call need not be in lexical scope. | |
def routine(n) | |
puts n | |
throw :done, "I'm the return value" if n <= 0 | |
routine(n-1) |
This file contains hidden or 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
# ruby 2.1.2 | |
require 'benchmark/ips' | |
Benchmark.ips do |x| | |
x.report { (1..100).select { |x| x % 3 == 0 }.select { |x| x % 4 == 0 } } | |
x.report { (1..100).select { |x| x % 3 == 0 && x % 4 == 0 } } | |
x.report { (1..100).lazy.select { |x| x % 3 == 0 }.select { |x| x % 4 == 0 }.to_a } | |
end |
This file contains hidden or 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
# http://ruby-doc.org/core-2.0/Proc.html#method-i-curry | |
# | |
# curry → a_proc | |
# | |
# Returns a curried proc. | |
# | |
# curry(arity) → a_proc | |
# | |
# If the optional arity argument is given, it determines the number of arguments. | |
# |