Cool tricks to do with Ruby, Rails and Pry: Date Parse, WTF, Safe Navigation Operator

23 October 2016 on . 5 minutes to read

Not the machete kind of hacking.

Find Cool Hacks for your Work

Once every few weeks, I find contemplating a new feature or project I’d like to pursue either for a client or myself. Often, this involves building out a simple proof of concept. This is the path my thought process will go down:

  1. Build a mental picture, start to finish of the large building blocks (The high level, architectural view)
  2. For each large feature, quickly run through the individual implementation details in my head and identify the process needed to build that larger feature out
  3. Connect the dots

Invariably, there will be some detail that escapes my current knowhow. These days I typically solve that between reading source code and hacking around in Pry. What follows is a list of a few cool tricks and observations I’ve run across while finding solutions to future development. There will be more to come as I expand on it.




‘Date.parse` pulling a date out of a long url (from a site spidering project):

Date.parse("http://blog.nrowegt.com/2016/10/20/modeling-postgres-common-table-expressions-and-window-functions-with-rails-and-activerecord.html")
Thu, 20 Oct 2016

When you get an exception wtf? it to get a stacktrace as well as suggestions. Extremely helpful for failing tests or debugging on a production console when you’re in a hurry.

NoMethodError: undefined method 'to_a' for "asdf":String
from (pry):1:in '__pry__'
[3] pry(main)> wtf?
Exception: NoMethodError: undefined method 'to_a' for "asdf":String
Did you mean?  to_yaml
               to_r
               to_f
               to_i
               to_s
               to_c
--
0: (pry):1:in '__pry__'
1: ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in 'eval'
2: ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in 'evaluate_ruby'
3: ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in 'handle_line'
4: ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in 'block (2 levels) in eval'
5: ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in 'catch'
6: ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in 'block in eval'
7: ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in 'catch'
8: ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in 'eval'
9: ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/pry-0.10.4/lib/pry/repl.rb:77:in 'block in repl'

Ruby 2.3 introduced &:, aka the safe navigation operator, or lonely operator.

Try obj&.method to replace your .try(:method):

[13] pry(main)> nil.fly
NoMethodError: undefined method 'fly' for nil:NilClass
from (pry):11:in '__pry__'
[14] pry(main)> nil&.fly
nil

If you enjoy having free time and the peace of mind that a professional is on your side, then you’d love to have me work on your project.

Contact or view a list of available services to see how I’ll make your life better, easier and bring satisfaction back into you running your business.