ruby tips and tricks

20
RUBY Tips & Tricks Diogo Scudeei - 2015

Upload: diogo-scudelletti

Post on 15-Jul-2015

275 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Ruby Tips and Tricks

RUBY Tips & Tricks

Diogo Scudelletti - 2015

Page 2: Ruby Tips and Tricks

WHO Am I?TWITTER: DSCUDELLETTI

GITHUB: SCUDELLETTIBANKFACIL (We are Hiring!)

Diogo Scudelletti - 2015

Page 3: Ruby Tips and Tricks

Cautionary WarningThe code in this presentation is to illustrate some awesome features in

ruby, not necessarily they should be used in production.Feel free to use them as your wish though... !

Diogo Scudelletti - 2015

Page 4: Ruby Tips and Tricks

GET LAST EXECTIONbegin raise 'hahahaha'rescue => e # Show the File, Line and Error for the exceptions puts [email protected]

Returns an Array with the Stracktrace["last_exception.rb:2:in `<main>'"]

Diogo Scudelletti - 2015

Page 5: Ruby Tips and Tricks

LISTING LOCAL VARIABLESsome_variable = 1another_variable = 2

puts local_variables.inspect # => [:some_variable, :another_variable]

def something hidden_variable = 3 puts local_variables.inspect # => [:hidden_variable]end

something

puts local_variables.inspect # => [:some_variable, :another_variable]

Diogo Scudelletti - 2015

Page 6: Ruby Tips and Tricks

LISTING THINGS USING GARBAGE COLLECTORList all objects

x=[];ObjectSpace.each_object{|a| x<<a} # => ["RubyVM::InstructionSequence:has_unit_tests...", ...]

List all classesx=[];ObjectSpace.each_object(Class){|a| x<<a}# => ["ARGF.class", "ArgumentError", "Array", "BasicObject", ...]

Diogo Scudelletti - 2015

Page 7: Ruby Tips and Tricks

GSUB + HASH == ❤hash = {"o" => 0, "e" => 3}regexp = Regexp.new(hash.keys.join("|"))

"Love".gsub(regexp, hash) # => "L0v3"

Diogo Scudelletti - 2015

Page 8: Ruby Tips and Tricks

METHODS THE WIN!def some_method(number) "Magic Number: #{number}"end

[1,2,3].map(&method(:some_method))# => ["Magic Number: 1", "Magic Number: 2", "Magic Number: 3"]

PS: It works with Lambdas and Procs

Diogo Scudelletti - 2015

Page 9: Ruby Tips and Tricks

BLOCK ARITYdef some_method(&block) case block.arity when 0 block.call when 1 block.call 'one' when 2 block.call 'one', 'two' when 3 block.call 'one', 'two', 'three' endend

some_method{ |a| puts "#{a}" } # => onesome_method{ |a,b| puts "#{a} #{b}" } # => one twosome_method{ |a,b,c| puts "#{a} #{b} #{c}" } # => one two three

Diogo Scudelletti - 2015

Page 10: Ruby Tips and Tricks

INLINE PRIVATE METHODSclass SomeClass private def some_method puts "XPTO" endend

SomeClass.new.some_method# => NoMethodError: private method `some_method' called

Diogo Scudelletti - 2015

Page 11: Ruby Tips and Tricks

CURRYING FOR THE WIN!calculator = -> (method, a, b) do a.send(method, b)end

calculator.call(:+, 1, 2)calculator.call(:-, 1, 2)

sum = calculator.curry.call(:+)sub = calculator.curry.call(:-)

sum.call(1,2)sub.call(1,2)

Diogo Scudelletti - 2015

Page 12: Ruby Tips and Tricks

DESTRUCTURING ❤a,b,c = [1,2,3]a, = [4,5,6]a, *b = [1,2,3,4]

{ one: [1,2,3], two: [4,5,6] }.each do |key, (first, second, third)| puts "#{key}: #{first} - #{second} - #{third}"end

{ one: [1,2,3], two: [4,5,6] }.each do |key, (head, *tail)| puts "#{key}: #{head} - #{tail}"end

PS Also works with arraysDiogo Scudelletti - 2015

Page 13: Ruby Tips and Tricks

HOW TO CALL A LAMBDA/PROC-> (a){p a}.call("Hello World")-> (a){p a}["Hello World"]-> (a){p a}.("Hello World")

Choose the most readable one!

Diogo Scudelletti - 2015

Page 14: Ruby Tips and Tricks

DUP vs CLONEobject = Object.new.freezeobject.dup.frozen? #=> falseobject.clone.frozen? #=> true

Diogo Scudelletti - 2015

Page 15: Ruby Tips and Tricks

REMOVE vs UNDEFPart 1

class Parent def hello puts "In parent" endendclass Child < Parent def hello puts "In child" endend

Diogo Scudelletti - 2015

Page 16: Ruby Tips and Tricks

REMOVE vs UNDEFPart 2

c = Child.newc.hello # => In child

class Child remove_method :hello # remove from child, still in parentendc.hello # => In parent

class Child undef_method :hello # prevent any calls to 'hello'endc.hello # => undefined method `hello'

Diogo Scudelletti - 2015

Page 17: Ruby Tips and Tricks

METAPROGRAMMING STUFF - REFINEMENTPart 1

class Fixnum def to_hex self.to_s(16) endend

module ExtendFixnum refine Fixnum do def to_octal self.to_s(8) end endend

Diogo Scudelletti - 2015

Page 18: Ruby Tips and Tricks

METAPROGRAMMING STUFF - REFINEMENTPart 2

class Conversor using ExtendFixnum

def self.convert_to_hex 10.to_hex end

def self.convert_to_octal 10.to_octal endend

Diogo Scudelletti - 2015

Page 19: Ruby Tips and Tricks

METAPROGRAMMING STUFF - REFINEMENTPart 3

--- INSIDE CLASS ---HEX:aOCTAL:12--- OUTSIDE CLASS ---HEX:arefinement.rb:35:in `<main>': undefined method `to_octal' for 10:Fixnum (NoMethodError)

Diogo Scudelletti - 2015

Page 20: Ruby Tips and Tricks

FinTWITTER: DSCUDELLETTI

GITHUB: SCUDELLETTISeriously We are Hiring!

Diogo Scudelletti - 2015