0.0
No commit activity in last 3 years
No release in over 3 years
Changes Sinatra params hash to use HashWithIndifferentAccess
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

>= 0.9.0
 Project Readme

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.