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

  • Home
  • SEARCH
  • 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 7767219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:37:17+00:00 2026-06-01T15:37:17+00:00

I have a scenario that when I try to access a hash key using

  • 0

I have a scenario that when I try to access a hash key using a symbol it doesn’t work, but when I access it with a string it works fine. It is my understanding that symbols are recommended over strings, so I am trying to clean my script. I am using hash symbols elsewhere in my script, it is just this specific scenario that does not work.

Here is the snippet:

account_params ={}
File.open('file', 'r') do |f|
  f.each_line do |line|
    hkey, hvalue = line.chomp.split("=")
    account_params[hkey] = hvalue
  end
end

account_scope = account_params["scope"]

This works, however if I use a symbol it doesn’t, as shown below:

account_scope = account_params[:scope]

When I use a symbol I get:

can't convert nil into String (TypeError)

I am not sure if it matters, but the contents of this specific hash value looks something like this:

12345/ABCD123/12345
  • 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-01T15:37:19+00:00Added an answer on June 1, 2026 at 3:37 pm

    The key you’re reading in from the file is a string. In fact, everything you’re reading in from the file is a string. If you want the keys in your hash to be symbols, you could update the script the do this instead:

    account_params[hkey.to_sym] = hvalue
    

    This will turn “hkey” into a symbol instead of using the string value.

    Ruby provides a variety of these types of methods which will coerce the value into a different type. For instance: to_i will take it to an integer, to_f to a float, and to_s will take something back to a string value.

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

Sidebar

Related Questions

Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
The scenario I have access to this old linux server, that no longer receives
I have a fairly complex scenario that I try to port to Windows 8
I have a path defined: when /the admin home\s?page/ /admin/ I have scenario that
I have a scenario that my load balancer translates port 80 from outside into
I have a scenario that I haven't been able to solve: I'm toying around
I have a scenario that, I am creating dynamic html content and I need
I have a scenario outline table that looks like the following: Scenario Outline: Verify
Here is the scenario that I have. I have a cvs repository in one
I'm trying to write a Cucumber scenario that requires me to have a logged

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.