dangerous ruby (or: job security)
TRANSCRIPT
ARRAY SLICE arr = [:one, :two, :three, :four]arr[1] #=> :twoarr[1,2] #=> [:two, :three]arr[6,2] #=> nilarr[4,2] #=> []
SEPARATOR %w(a b c) #=> ["a", "b", "c"]%w|a b c| #=> ["a", "b", "c"]
%w^a b c^ #=> ["a", "b", "c"]
%w%a b c% #=> ["a", "b", "c"]
IMPLICIT BLOCK class Wut # by @benlovell def initialize @proc = Proc.new end def call_it @proc.call end end lol = Wut.new { puts 'lolwut' } lol.call_it #=> "lowlut"
IMPLICIT BLOCK "If Proc.new is called from inside a method without any arguments of its own, it will return a new Proc containing the block given to its surrounding method."
http://mudge.name/2011/01/26/passing-blocks-in-ruby-without-block.html
ALIAS METHODclass User def full_name puts "Winston Smith" end
def self.add_rename alias_method :name, :full_name end end
ALIAS METHODclass Developer < User def full_name puts "Geeky geek" end add_rename end
Developer.new.name #=> 'Gekky geek'
ALIAS METHODclass Developer < User def full_name puts "Geeky geek" end add_rename end
Developer.new.name #=> 'Winston Smith'
class Developer < User def full_name puts "Geeky geek" end add_rename end
ALIAS METHOD"alias is a keyword and is lexically scoped. It means it treats self as the value of self at the time the source code was read .
alias_method treats self as the value determined at the run time."
http://blog.bigbinary.com/2012/01/08/alias-vs-alias-method.html
SPACEY METHOD NAMEclass SpaceInvader class << self define_method("your spacey\n name") do
"foo" "bar" end
end end
SpaceInvader.methods - Object.methods #=> your spacey name
PARAMSdef detector(b=(default=true; 1)) "b=#{b} default=#{default.inspect}" end
detector #=> "b=1 default=true"
detector(1) #=> "b=1 default=nil"
PARAMSdef dont_use_without_args!(
a = (Fixnum.send(:define_method, :+) do |other| self - other;
end; "I'll be back"))
a end
dont_use_without_args! #=> "I'll be back"
1 + 2 #=> -1
QUESTIONS?Vidmantas Kabošis
Backend Rubyist @ Toptal
@vidmantas
Toptal community gathering - May 19th (Tuesday) bit.ly/toptal-vilnius