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 8621187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:45:51+00:00 2026-06-12T06:45:51+00:00

I’m stepping through some Ruby code of a hangman app made with Sinatra. Specifically,

  • 0

I’m stepping through some Ruby code of a hangman app made with Sinatra. Specifically, I’m looking at the post "/check" do path/function below. The first two steps of the function are predictable, namely, it shows the final_word variable, and then the char_clicked variable, but then the debugger reveals this Hash.new

Hash.new {|hash,key| hash[key.to_s] if Symbol === key }

before moving on in the function. I don’t see why post "/check" is creating a new hash at this point. It’s not explicit in the code, and it’s not obvious to me why this would be happening behind the scenes. I included the correct_guess method below as well, which gets invoked in “

final_word = session[:word]
(rdb:3) n
hangman.rb:79
char_clicked = params[:char_clicked]
(rdb:3) n
/Users/mm/.rvm/gems/ruby-1.9.2-p290@rails3tutorial/gems/sinatra-1.3.3/lib/sinatra/base.rb:916
Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
(rdb:3) n
hangman.rb:80
correct_guess = Game.correct_guess?(char_clicked, final_word)

check path

post "/check" do
  debugger
  final_word = session[:word]
  char_clicked = params[:char_clicked]
  correct_guess = Game.correct_guess?(char_clicked, final_word)

  if correct_guess
    session[:revealed_word] = Word.reveal(session[:revealed_word], char_clicked, final_word)
    session[:chars_left] = Word.chars_left(session[:revealed_word])
  else
    session[:incorrect_guesses] += 1
  end
  win = Game.win?(session[:chars_left], session[:incorrect_guesses])

  {:word => session[:revealed_word], :correct_guess => correct_guess, :incorrect_guesses => session[:incorrect_guesses], :win => win}.to_json
end

Game correct_guess method

def correct_guess?(char_clicked, final_word)
  final_word.include?(char_clicked)
end
  • 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-12T06:45:53+00:00Added an answer on June 12, 2026 at 6:45 am

    Sinatra is creating the params hash for ‘indifferent access’ (so the hash keys can be accessed by string or symbols interchangeably).

    Hash.new takes a block to determine what value to return if the key doesn’t exist.

    Example:

    >> sh = Hash.new
    => {}
    >> sh["key"] = "value"
    => "value"
    >> sh[:key]
    => nil
    
    >> ih = Hash.new { |hash, key| hash[key.to_s] if Symbol === key }
    => {}
    >> ih["key"] = "value"
    => "value"
    >> ih[:key]
    => "value"
    

    When you try to access params[:char_clicked] the hash block executes so the debugger dumps the full line containing the block (the hash isn’t actually being created at that point):

    h = Hash.new { |hash, key| hash[key.to_s] if Symbol === key }
    debugger
    h["invalid_key"]
    

    ->

    sample.rb:5
    h["invalid_key"]
    (rdb:1) n
    sample.rb:3
    h = Hash.new { |hash, key| hash[key.to_s] if Symbol === key }
    

    When the block is on a separate line the it’s easier to see which part of the code is being executed (i.e. just the block):

    h = Hash.new do 
      |hash, key| hash[key.to_s] if Symbol === key
    end
    debugger
    h["invalid_key"]
    

    ->

    sample.rb:7
    h["invalid_key"]
    (rdb:1) n
    sample.rb:4
    |hash, key| hash[key.to_s] if Symbol === key
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.