=> "HELLO" a.__send__(:upcase) # => "HELLO" class Foo private def my_secret puts "I love Ruby" end end f = Foo.new f.my_secret # => NoMethodError f.send(:my_secret) # => "I love Ruby" Monday, May 6, 13
b = 'This is not dynamic, now is #{Time.now}' puts a => My dynamic string, now is 2013-04-01 16:57:30 -0300 puts b => This is not dynamic, now is #{Time.now} %q(Interpolationless string 'foo', "bar" & #{Time.now}) => "Interpolationless string 'foo', \"bar\" & \#{Time.now}" %Q(Interpolated string 'foo', "bar" & #{Time.now}) => "Interpolated string 'foo', \"bar\" & 2013-04-01 17:02:02 -0300" Monday, May 6, 13
line.split(/\s+/) break if %w(quit q exit).include?(words.first) random_words.add(words.sample.to_sym) end puts random_words A possible out of memory attack could be done Monday, May 6, 13
same object. == Tests if two values are equals. eql? Alias for equal?. Used as a stricted version of ==. === Case equality. Used in case statement to check if target matches any of the when clauses Monday, May 6, 13
instance_method puts "Instance method" end end Methods class B class << self def class_method_1 puts "Class Method 1" end def class_method_2 puts "Class Method 2" end end end Monday, May 6, 13
def bar puts "Bar from A" end end class B < A def foo super puts "Foo from B" end def bar puts "Bar from B" end end a = A.new b = B.new a.foo a.bar # => Foo from A # => Bar from A b.foo b.bar # => Foo from A # => Foo from B # => Bar from B Monday, May 6, 13
"m1" end def my_method_2 puts "m2" end end f1 = Foo.new f2 = Foo.new def f1.foo puts "foo! from f1" end f1.foo # => foo! from f1 f2.foo # => NoMethodError Singleton Methods Monday, May 6, 13
singleton_class #MyClass class_method class MyClass def self.class_method puts 1 end def instance_method puts 2 end end obj = MyClass.new def obj.custom_method puts 3 end singleton_class Monday, May 6, 13
end def bar puts "Bar from Module" end end class MyClass include MyModule include MyOtherModule def foo puts "Foo from #{self}" super end end obj = MyClass.new obj.bar # => Bar from Module obj.foo # => Foo from MyClass # => Foo from OtherModule # => Foo from Module module MyOtherModule def foo puts "Foo from OtherModule" super end end Monday, May 6, 13
end def bar puts "Bar from Module" end end class MyClass include MyModule prepend MyOtherModule def foo puts "Foo from #{self}" super end end obj = MyClass.new obj.bar # => Bar from Module obj.foo # => Foo from OtherModule # => Foo from MyClass # => Foo from Module module MyOtherModule def foo puts "Foo from OtherModule" super end end Ruby 2.0 Monday, May 6, 13
at #{Time.now}") @first_name end def first_name=(first_name) logger.info("First name was changed from #{@first_name} to #{first_name}") @first_name = first_name end def last_name logger.info("Last name was fetched at #{Time.now}") @last_name end def last_name=(last_name) logger.info("Last name was changed from #{@last_name} to #{last_name}") @last_name = last_name end end Monday, May 6, 13
module InstanceMethods end module ClassMethods def attr_auditable(*attributes) attributes.each do |attribute| define_setter(attribute) define_getter(attribute) end end ... end end Monday, May 6, 13
module InstanceMethods end module ClassMethods def attr_auditable(*attributes) attributes.each do |attribute| define_setter(attribute) define_getter(attribute) end end ... end end private def define_setter(attribute) var_name = "@#{attribute}".to_sym define_method("#{attribute}=") do |value| old_value = instance_variable_get(var_name) puts("#{attribute} was changed from #{old_value} to #{value} at #{Time.now}") instance_variable_set(var_name, value) end end def define_getter(attribute) var_name = "@#{attribute}".to_sym define_method(attribute) do puts("#{attribute} was fetched at #{Time.now}") instance_variable_get(var_name) end end Monday, May 6, 13
called with arguments #{args}" end end repository = UserRepository.new(Recorder.new) repository.find_by_name("john") # => Method execute called with arguments SELECT * FROM users WHERE name = "john" Monday, May 6, 13
module InstanceMethod def method_missing(method, *args) delegate = send(@@delegate) delegate.send(method, *args) end end module ClassMethod def delegate_to(delegate) @@delegate = delegate end end end Monday, May 6, 13
def initialize(secretary) @secretary = secretary end end class Secretary def make_finance_report Report.new end end secretary = Secretary.new boss = Boss.new(secretary) report = boss.make_finance_report Monday, May 6, 13
obj = Foo.new var = obj.instance_eval { @var } # => 30 obj.instance_eval do def var @var end end foo.var # => 30 Foo.new.var # => NoMethodError Foo.class_eval do def var=(var) @var = var end end foo.var = 40 foo.var # => 40 Foo.new.var = 50 Monday, May 6, 13
obj = Foo.new var = obj.instance_eval { @var } # => 30 obj.instance_eval do def var @var end end foo.var # => 30 Foo.new.var # => NoMethodError Foo.class_eval do def var=(var) @var = var end end foo.var = 40 foo.var # => 40 Foo.new.var = 50 Monday, May 6, 13