Top Banner
DANGEROUS RUBY
29

Dangerous Ruby (or: Job Security)

Jul 21, 2015

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dangerous Ruby (or: Job Security)

DANGEROUS RUBY

Page 2: Dangerous Ruby (or: Job Security)

THE PRINCIPLE OF LEAST SURPRISE

Matz's idea of "least astonishment"

Not yours

Page 3: Dangerous Ruby (or: Job Security)

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

Page 4: Dangerous Ruby (or: Job Security)

OCTAL NUMBERS p "high #{5}"

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

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

Page 5: Dangerous Ruby (or: Job Security)

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

blerp += 1 #=> 1

Page 6: Dangerous Ruby (or: Job Security)

OPERATOR PRECEDENCE a = nil #=> nil

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

b #=> nil

Page 7: Dangerous Ruby (or: Job Security)

DEFINED? defined?(x) #=> nil

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

x #=> NameErrorif false x = 1 end

Page 8: Dangerous Ruby (or: Job Security)

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"]

Page 9: Dangerous Ruby (or: Job Security)

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"

Page 10: Dangerous Ruby (or: Job Security)

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

Page 11: Dangerous Ruby (or: Job Security)

IMPLICIT BLOCK def speak puts Proc.new.call end

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

Page 12: Dangerous Ruby (or: Job Security)

ALIASESmap / select

detect / find

alias / alias_method ...

def full_name 'Winston Smith' end

Page 13: Dangerous Ruby (or: Job Security)

ALIAS METHODalias name full_name

alias_method :name, :full_name

name #=> "Wintston Smith"

Page 14: Dangerous Ruby (or: Job Security)

ALIAS METHODclass User def full_name puts "Winston Smith" end

def self.add_rename alias_method :name, :full_name end end

Page 15: Dangerous Ruby (or: Job Security)

ALIAS METHODclass Developer < User def full_name puts "Geeky geek" end add_rename end

Developer.new.name #=> 'Gekky geek'

Page 16: Dangerous Ruby (or: Job Security)

ALIAS METHODclass User def self.add_rename alias :name :full_name end end

Page 17: Dangerous Ruby (or: Job Security)

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

Page 18: Dangerous Ruby (or: Job Security)

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

Page 19: Dangerous Ruby (or: Job Security)

THE PRINCIPLE OF LEAST SURPRISE

Page 20: Dangerous Ruby (or: Job Security)

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

Page 21: Dangerous Ruby (or: Job Security)

PARAMSdef detector(b=(default=true; 1)) "b=#{b} default=#{default.inspect}" end

detector #=> "b=1 default=true"

detector(1) #=> "b=1 default=nil"

Page 22: Dangerous Ruby (or: Job Security)

PARAMSdef foo(a, b=def foo(a); "F"; end) a end

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

Page 23: Dangerous Ruby (or: Job Security)

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

Page 24: Dangerous Ruby (or: Job Security)
Page 25: Dangerous Ruby (or: Job Security)
Page 26: Dangerous Ruby (or: Job Security)
Page 27: Dangerous Ruby (or: Job Security)

RECAPhighly dynamic

productive

unpredictable

dangerous

THE PRINCIPLE OF LEAST SURPRISE

Page 28: Dangerous Ruby (or: Job Security)

RECAPhighly dynamic

productive

unpredictable

dangerous

THE PRINCIPLE OF LEAST SURPRISE (KIND OF)

Page 29: Dangerous Ruby (or: Job Security)

QUESTIONS?Vidmantas Kabošis

Backend Rubyist @ Toptal

@vidmantas

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