dangerous ruby (or: job security)

29
DANGEROUS RUBY

Upload: vidmantas-kabosis

Post on 21-Jul-2015

141 views

Category:

Software


0 download

TRANSCRIPT

DANGEROUS RUBY

THE PRINCIPLE OF LEAST SURPRISE

Matz's idea of "least astonishment"

Not yours

ARRAY SLICE arr = [:one, :two, :three, :four]arr[1] #=> :twoarr[1,2] #=> [:two, :three]arr[6,2] #=> nilarr[4,2] #=> []

OCTAL NUMBERS p "high #{5}"

p "high #{071}" #=> "high 57"

p "high #{098}" #=> Invalid octal digit

NIL OBJECT x += 1 #=> NoMethodErrordef nil.+(other) other end

blerp += 1 #=> 1

OPERATOR PRECEDENCE a = nil #=> nil

b = a or "default" #=> "default"

b #=> nil

DEFINED? defined?(x) #=> nil

defined?(x) #=> "local-variable"

x #=> NameErrorif false x = 1 end

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

IMPLICIT BLOCK def speak puts Proc.new.call end

speak { "Hello" } #=> "Hello"

ALIASESmap / select

detect / find

alias / alias_method ...

def full_name 'Winston Smith' end

ALIAS METHODalias name full_name

alias_method :name, :full_name

name #=> "Wintston Smith"

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 User def self.add_rename alias :name :full_name end end

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

THE PRINCIPLE OF LEAST SURPRISE

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 foo(a, b=def foo(a); "F"; end) a end

foo("W", 1) + foo("T") + foo("bar") #=> "WTF"

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

RECAPhighly dynamic

productive

unpredictable

dangerous

THE PRINCIPLE OF LEAST SURPRISE

RECAPhighly dynamic

productive

unpredictable

dangerous

THE PRINCIPLE OF LEAST SURPRISE (KIND OF)

QUESTIONS?Vidmantas Kabošis

Backend Rubyist @ Toptal

@vidmantas

Toptal community gathering - May 19th (Tuesday) bit.ly/toptal-vilnius