5 MoAR Ruby Tips

Yinquan (Yink) Teo

twitter: @yinquanteo
github: github.com/yinquanteo
url: yinquanteo.com

1. Underscores In Ruby Numbers


>> 1_000     # => 1000
>> 1_000_000 # => 1000000
    

2.  Local Variable Assignment in Conditional Body


x 
# NameError: undefined local variable or method `x'

if false
  x = 5
end
x # => nil  

3. TurnOUt


gem 'turnout'

rake maintenance:start

rake maintenance:start reason="be back in a jiffy"
https://github.com/biola/turnout

4. Retry


def deliver_email
  # ...
  
  retries = 0
  begin
    new_mail.deliver
  rescue Timeout::Error => e
    if retries < 5 
      retries += 1
      sleep (2 ** retries)
      retry
    else
      raise
    end 
  end 
end
      

4.1 Retry - Retryable Gem


def deliver_email
  # ...
  
  retryable(tries: 5, sleep: lambda {|n| 2**n}, on: Timeout::Error) do
    new_mail.deliver
  end
end
      

5. RubyCritic


https://github.com/whitesmith/rubycritic

Demo

QuESTIONS?

5 Moar Ruby Tips

By yinquanteo

5 Moar Ruby Tips

  • 775
Loading comments...

More from yinquanteo