ruby tips and tricks
TRANSCRIPT
RUBY Tips & Tricks
Diogo Scudelletti - 2015
WHO Am I?TWITTER: DSCUDELLETTI
GITHUB: SCUDELLETTIBANKFACIL (We are Hiring!)
Diogo Scudelletti - 2015
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
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
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
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
GSUB + HASH == ❤hash = {"o" => 0, "e" => 3}regexp = Regexp.new(hash.keys.join("|"))
"Love".gsub(regexp, hash) # => "L0v3"
Diogo Scudelletti - 2015
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
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
INLINE PRIVATE METHODSclass SomeClass private def some_method puts "XPTO" endend
SomeClass.new.some_method# => NoMethodError: private method `some_method' called
Diogo Scudelletti - 2015
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
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
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
DUP vs CLONEobject = Object.new.freezeobject.dup.frozen? #=> falseobject.clone.frozen? #=> true
Diogo Scudelletti - 2015
REMOVE vs UNDEFPart 1
class Parent def hello puts "In parent" endendclass Child < Parent def hello puts "In child" endend
Diogo Scudelletti - 2015
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
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
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
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
FinTWITTER: DSCUDELLETTI
GITHUB: SCUDELLETTISeriously We are Hiring!
Diogo Scudelletti - 2015