Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7862961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:19:36+00:00 2026-06-02T23:19:36+00:00

I was writing a quick helper in Sinatra for redirect_to_next , where I redirect

  • 0

I was writing a quick helper in Sinatra for redirect_to_next, where I redirect to the path provided by session[:next] if it exists, or a default.

In Sinatra, session really provided by Rack, and by spec, it is said to provide a hash like interface for fetch. I wrote the following error helper to explain my problem.

error 401 do
  session[:next] = request.path
  puts "get   #{session[:next]}"
  puts "fetch #{session.fetch(:next, '/')}"
  redirect "/login"
end

When I attempt to access /settings when not logged in, I halt 401 which runs the above code. This is what it prints to my terminal:

get   /settings
fetch /

:next exists as a key, so why is it giving me the default as if it does not?

Update

This minimal example shows the same behavior.

require 'sinatra'

set :sessions, true

get '/' do
  session[:testing] = "hello"
  puts "get   #{session[:testing]}"
  puts "fetch #{session.fetch(:testing, 'goodbye')}"
end

Logs

[2012-04-29 14:11:51] INFO  WEBrick::HTTPServer#start: pid=1954 port=9292
get   hello
fetch goodbye
10.0.2.2 - - [29/Apr/2012 14:11:54] "GET / HTTP/1.1" 200 - 0.0485

Software

  • ruby (1.9.3p194)
  • rack (1.4.1)
  • sinatra (1.3.2)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:19:37+00:00Added an answer on June 2, 2026 at 11:19 pm

    The session hash isn’t a normal Ruby Hash, it’s a Rack::Session::Abstract::SessionHash. SessionHash actually inherits from Hash, but it overrides the []= and [] methods, calling to_s on any keys before storing and retrieving them.

    Extending your update example:

    require 'sinatra'
    
    set :sessions, true
    
    get '/' do
      session[:testing] = "hello"
      puts "get               #{session[:testing]}"
      puts "fetch             #{session.fetch(:testing, 'goodbye')}"
      puts "fetch with string #{session.fetch(:testing.to_s, 'goodbye')}"
    end
    

    gives this output:

    get               hello
    fetch             goodbye
    fetch with string hello
    

    When you use Hash#fetch, passing a symbol, the method gets dispatched directly to the parent hash, without being converted to a string, and so the matching key isn’t found.

    So, always use Strings as keys in your sessions and everything should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Quick question. I am writing a website and would like to redirect users to
I'm writing a quick server app for something so don't really want to write
Just a quick question really. I am re-writing a site and I am converting
I am writing a really simple app which should be quick to use. I
now this is embarrassing. I'm writing quick script and I can't figure out why
I'm writing a quick and dirty maintenace script to delete some rows and would
Is this a simple process? I'm only writing a quick hacky UI for an
Quick question about Wordpress PHP: I am writing a theme and I want to
I'm writing a quick shell script to build and execute my programs in one
I was writing a quick one-liner in GHCi and tried to compose sum with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.