Title / Description
Code require 'timeout' class TimeoutWrapper def initialize(timeout_in_seconds, target) @timeout = timeout_in_seconds @target = target end def respond_to?(symbol, include_private=false) @target.respond_to?(symbol, include_private) end def method_missing(method_name, *args) status = Timeout::timeout(5) { @target.send(method_name, args) } end end class Trip < ActiveRecord::Base # Crappy backport of 'where' to rails 2.3 def self.where(attribute_map) keys = attribute_map.keys.sort composed_name = keys.map(&:to_s).join('_and_') values = keys.map{|key|attribute_map[key]} send("find_by_#{composed_name}", values) end end Trip.where(:origin => 'ORD', :destination => 'SFO') send("#{state}_price") class Klass def hello(*args) "Hello " + args.join(' ') end end k = Klass.new k.send :hello, "gentle", "readers" #=> "Hello gentle readers" class ClasslessObject def self.new(attributes) clazz = Class.new attributes.keys.each do |key| value = attributes[key] callable = value callable = lambda { || value } if !value.respond_to?(:call) clazz.send(:define_method, key, callable) end clazz.new end end object = ClasslessObject.new( :value_per_unit => 10, :quantity => 3, :total => lambda {|| value_per_unit * quantity} ) object.methods - Object.new.methods other = ClasslessObject.new( :value_per_unit => 15, :total => lambda {|number| value_per_unit * number} ) other.methods - Object.new.methods class Integer def to_roman # The answer 'XLII' # Guaranteed to NOT be random end end Math::PI => 3.14159265358979 Math.const_set('PI', 22.0/7) Math::PI => 3.14285714285714
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code