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
# Hmmm... that looks somehow familiar. | |
# | |
module Living extend Trait | |
# ... | |
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
# H h hello, I am a p p p proc… | |
# | |
p=->(p){p p} | |
# p=->(p){p[p]} | |
p[p] |
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
# So this is possible in Ruby 1.9 | |
# | |
def test a, b, c = :c, *d, e | |
p [a,b,c,d,e] | |
end | |
# Run this to see how it works. | |
# | |
(4..7).inject([1,2,3]) do |current_params, next_param| | |
p test(*current_params) |
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
class Strategy | |
def self.do what, to_what | |
define_method :do do |text| | |
text.gsub! what, to_what | |
end | |
end | |
def do text | |
# Implement a default behaviour. | |
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
class Symbol | |
def to_a | |
proc { |*args| eval("#{self}(#{args.join(',')})") } | |
end | |
end | |
a = ["hello", "HeLlO"] | |
a.each(*:p) | |
# Hey, at least I tried, right? Right? |
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 1.9 only. | |
# | |
*a, b = [1,2,3,4] | |
p :upper_example | |
p a | |
p b | |
a, *b, c = [1,2,3,4] | |
p :lower_example |
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
# Creates beautiful mountain ranges. | |
# | |
class MountainRange | |
def up offset = 0 | |
print '\\', offset | |
up_or_down? offset+1 | |
up_or_down? offset | |
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
# From http://eigenclass.org/R2/writings/object-size-ruby-ocaml: | |
# | |
# Ruby 1.9 stores up to 3 instance variables in | |
# the object slot without using an external table, | |
# so an object with one IV will only take 5 words. | |
# Beyond 3 instance variables, it reverts to an | |
# external IV array which is resized exponentially | |
# (factor 1.25) as new elements are added. | |
# | |
# Run with 1.9. |
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 self.it_should_be_greater left, right, expected = true | |
it "should be #{expected} if #{left} is greater than #{right}" do | |
Logic::Predicate::GreaterThan.eval(left, right).should == expected | |
end | |
end | |
... | |
it_should_be_greater 2, 1 | |
it_should_be_greater 2, 1, false |
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
class MysqlError < StandardError | |
end | |
class MysqlError < StandardError | |
end | |
def test | |
raise MysqlError |