Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ruby Tips and Tricks

869 views

Published on

# Ruby Tips & Tricks

---

# Who **_Am_** I?

### Twitter: dscudelletti
### GitHub: scudelletti
### BankFacil **_(We are Hiring!)_**

---

# **_ Cautionary Warning _**

The code in this presentation is to illustrate some awesome features in ruby, not necessary they should be used in production.
Feel free to use them as your wish though... :smile:

Topics:
* Get last Exection
* Listing Local Variables
* Listing Things using Garbage Collector
* GSub + Hash == :heart:
* Methods the Win!
* Block Arity
* Inline Private Methods
* Currying For the Win!
* Destructuring :heart:
* How to Call a Lambda/Proc
* Dup **_vs_** Clone
* remove **_vs_** undef
* Metaprogramming Stuff - Refinement

Published in: Software
  • Download over *12,000* fully detailed shed plans and start building your next shed easily and quickly. ✔✔✔ https://url.cn/I86oXShh
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Ruby Tips and Tricks

  1. 1. RUBY Tips & Tricks Diogo Scudelletti - 2015
  2. 2. WHO Am I?TWITTER: DSCUDELLETTI GITHUB: SCUDELLETTI BANKFACIL (We are Hiring!) Diogo Scudelletti - 2015
  3. 3. Cautionary Warning The 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
  4. 4. GET LAST EXECTION begin raise 'hahahaha' rescue => e # Show the File, Line and Error for the exceptions puts $@.inspect end Returns an Array with the Stracktrace ["last_exception.rb:2:in `<main>'"] Diogo Scudelletti - 2015
  5. 5. LISTING LOCAL VARIABLES some_variable = 1 another_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
  6. 6. LISTING THINGS USING GARBAGE COLLECTOR List all objects x=[];ObjectSpace.each_object{|a| x<<a} # => ["RubyVM::InstructionSequence:has_unit_tests...", ...] List all classes x=[];ObjectSpace.each_object(Class){|a| x<<a} # => ["ARGF.class", "ArgumentError", "Array", "BasicObject", ...] Diogo Scudelletti - 2015
  7. 7. GSUB + HASH == ❤ hash = {"o" => 0, "e" => 3} regexp = Regexp.new(hash.keys.join("|")) "Love".gsub(regexp, hash) # => "L0v3" Diogo Scudelletti - 2015
  8. 8. 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
  9. 9. BLOCK ARITY def 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' end end some_method{ |a| puts "#{a}" } # => one some_method{ |a,b| puts "#{a} #{b}" } # => one two some_method{ |a,b,c| puts "#{a} #{b} #{c}" } # => one two three Diogo Scudelletti - 2015
  10. 10. INLINE PRIVATE METHODS class SomeClass private def some_method puts "XPTO" end end SomeClass.new.some_method # => NoMethodError: private method `some_method' called Diogo Scudelletti - 2015
  11. 11. 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
  12. 12. 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 arrays Diogo Scudelletti - 2015
  13. 13. 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
  14. 14. DUP vs CLONE object = Object.new.freeze object.dup.frozen? #=> false object.clone.frozen? #=> true Diogo Scudelletti - 2015
  15. 15. REMOVE vs UNDEF Part 1 class Parent def hello puts "In parent" end end class Child < Parent def hello puts "In child" end end Diogo Scudelletti - 2015
  16. 16. REMOVE vs UNDEF Part 2 c = Child.new c.hello # => In child class Child remove_method :hello # remove from child, still in parent end c.hello # => In parent class Child undef_method :hello # prevent any calls to 'hello' end c.hello # => undefined method `hello' Diogo Scudelletti - 2015
  17. 17. METAPROGRAMMING STUFF - REFINEMENT Part 1 class Fixnum def to_hex self.to_s(16) end end module ExtendFixnum refine Fixnum do def to_octal self.to_s(8) end end end Diogo Scudelletti - 2015
  18. 18. METAPROGRAMMING STUFF - REFINEMENT Part 2 class Conversor using ExtendFixnum def self.convert_to_hex 10.to_hex end def self.convert_to_octal 10.to_octal end end Diogo Scudelletti - 2015
  19. 19. METAPROGRAMMING STUFF - REFINEMENT Part 3 --- INSIDE CLASS --- HEX:a OCTAL:12 --- OUTSIDE CLASS --- HEX:a refinement.rb:35:in `<main>': undefined method `to_octal' for 10:Fixnum (NoMethodError) Diogo Scudelletti - 2015
  20. 20. FinTWITTER: DSCUDELLETTI GITHUB: SCUDELLETTI Seriously We are Hiring! Diogo Scudelletti - 2015

×