Today I Learned

TIL, 2017-11-30

  • Should I just use Ruby fetch everywhere?. fetch with the second argument or a block can work.
  • fetch implementation: hash.key?(key) ? hash[key] : default

Reference

h = {
  'a' => :a_value,
  'b' => nil,
  'c' => false
}

h.fetch('a', :default_value) #=> :a_value
h.fetch('b', :default_value) #=> nil
h.fetch('c', :default_value) #=> false
h.fetch('d', :default_value) #=> :default_value

(h['a'] || :default_value) #=> :a_value
(h['b'] || :default_value) #=> :default_value
(h['c'] || :default_value) #=> :default_value
(h['d'] || :default_value) #=> :default_value
  • Read Shopify style guide.
  • This exists? email_with_name = format('%s <%s>', user.name, user.email)
  • gsub vs sub: sub only replaces the first occurrence of the pattern specified, where gsub does it for all occurrences. So its faster. Ruby has its roots in Perl which is like this. Reference
  • Squiggly heredoc.
  • Searching in strings: a = 'haha', a['ah'] or a['hehe'].

This project is maintained by daryllxd