Sinatra::Hashfix - Use HashWithIndifferentAccess for Sinatra params¶ ↑
Get your hash fix!
Sinatra only partially implements a Rails style params[] hash, which can lead to confusing behavior, since methods like has_key?() and delete() don’t work as you’d expect:
groups.google.com/group/sinatrarb/browse_thread/thread/af4b40e610d4daf/bc953ca6d118a882
This gem replaces the default Sinatra params hash with HashWithIndifferentAccess (from ActiveSupport). Purists may balk, but this is all about avoiding bugs.
Installation¶ ↑
You know this tune:
gem install sinatra-hashfix
If you are using a classic (one-file) Sinatra app, just add:
require 'sinatra/hashfix'
If you are using a modular Sinatra::Base app, you must also add:
register Sinatra::Hashfix
To the top of your application class.
Example¶ ↑
Request:
/my/route?foo=1
Without:
params[:foo] # 1 params.has_key?(:foo) # false params.has_key?('foo') # true
With:
params[:foo] # 1 params.has_key?(:foo) # true params.has_key?('foo') # true
It’s the little things in life that make me happy.
Author¶ ↑
Copyright © 2010 Nate Wiger. All Rights Reserved. Released under the Artistic License.