Skip to content

Instantly share code, notes, and snippets.

@davelyon
Created September 19, 2010 05:51
Show Gist options
  • Save davelyon/586449 to your computer and use it in GitHub Desktop.
Save davelyon/586449 to your computer and use it in GitHub Desktop.
# More ruby fun!
def power(base)
Proc.new {|x| base ** x }
end
base2 = power(2) # => #<Proc:0x00000001001569d0@-:4>
base2.call(8) # => 256
# Eigenclasses
class CrazyStuff
attr_reader :var # Read only!
def initialize
@var = "You can't write me"
end
private
def nonono()
return "BAD!"
end
end
eigen = CrazyStuff.new()
def eigen.var=(monkeyPatch)
@var = monkeyPatch
end
eigen.var="Ha!" # => "Ha!"
eigen.var # => "Ha!"
breakin = CrazyStuff.new()
breakin.instance_eval("@var=\"EVIL\"") # this is evil. But it works!
breakin.var # => "EVIL"
# Rubyisms...
def atLeastZero(someNumber)
[0,someNumber].max
end
atLeastZero(1) # => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment