Skip to content

Instantly share code, notes, and snippets.

View floere's full-sized avatar
😁
Open Sourcing

Florian R. Hanke floere

😁
Open Sourcing
View GitHub Profile
# Hmmm... that looks somehow familiar.
#
module Living extend Trait
# ...
end
# H h hello, I am a p p p proc…
#
p=->(p){p p}
# p=->(p){p[p]}
p[p]
# 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)
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
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?
# 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
# Creates beautiful mountain ranges.
#
class MountainRange
def up offset = 0
print '\\', offset
up_or_down? offset+1
up_or_down? offset
end
# 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.
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
class MysqlError < StandardError
end
class MysqlError < StandardError
end
def test
raise MysqlError