Flowdock

Notes posted by suzuki

RSS feed
February 4, 2016
0 thanks

Fetch with default value

You can specify default value as a fail-back if a hash doesn’t have a given key.

{a: false}.fetch(:b, true)
=> true
{a: false}.fetch(:a, true)
=> false

It is useful especially in the case where you want to set default value for a method.

def initialize(args)
  @foo = args.fetch(:foo, 1)
  @bar = args.fetch(:bar, 2)

  # This can take over the following way.
  # `@bar = args[:bar] || 2` 
  # => @bar is overwritten if caller specifies nil for :bar!
end
March 29, 2013
0 thanks
March 29, 2013
0 thanks

Edge case

Have look how #between? handle adge case. It’s different from DateTime’s.

Date.yesterday.between?(Date.yesterday, Date.tomorrow)
=> true

Date.tomorrow.between?(Date.yesterday, Date.tomorrow)
=> true
October 26, 2012
0 thanks

#performed? is an option when getting ActionController::DoubleRenderError

You can avoid `ActionController::DoubleRenderError (Can only render or redirect once per action)` with `#performed?`

For example

def index
  redirect_to not_found_path unless authenticated?
  render :action => 'update' unless performed?
end