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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:39:28+00:00 2026-05-27T01:39:28+00:00

Although I’ve been using Ruby 1.9 for a while, I only recently discovered the

  • 0

Although I’ve been using Ruby 1.9 for a while, I only recently discovered the newer hash syntax that’s now supported:

settings = {
  host: "localhost",
  port: 5984
}

As opposed to:

settings = {
  "host" => "localhost"
}

I like its similarity to JavaScript’s object noation, and looks a bit like JSON, so I’ll probably switch to using it with all my libraries, but I still want to support other users and my own projects which assume the old syntax.

So it really comes down to a fairly simple question of having to test for both symbols and strings. Is there an easy way to do both of these lines as one?

return true if settings["host"] and settings["db"]

return true if settings[:host] and settings[:db]
  • 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-05-27T01:39:28+00:00Added an answer on May 27, 2026 at 1:39 am

    Even in Ruby < 1.9, you could use symbols for keys. For example:

    # Ruby 1.8.7
    settings = { :host => "localhost" }
    puts settings[:host] #outputs localhost
    settings.keys[0].class # => Symbol
    

    Ruby 1.9 changes the way that you create hashes. It takes the key and converts it to a symbol for you, while eliminating the need for a hash rocket.

    # Ruby 1.9.2
    settings = { host: "localhost" }
    settings[:host] # => "localhost"
    settings.keys[0].class # => Symbol
    

    In both cases, if I try to access settings[:name] with settings["name"], I’m going to get nil. All Ruby 1.9 does is allow for a new way of creating hashes. To answer your question, you cannot, as far as I know, use the new {key: value} syntax if you want backwards compatibility with Ruby 1.8.

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

Sidebar

Related Questions

Although I've been using Scala for a while and have mixed it with Java
Although im using c# and the .net lib, im interested in a regex-only solution
Although I am using drupal since the D4 series, I only started developing professionally
Although I've been programming for a few years I've only really dabbled in the
Although I grasp the concept of Bitwise Operators, I can't say that I have
Although using SQL FOR XML EXPLICIT is cumbersome, I find myself using it often.
Although I currently only have one app out in the App store, I have
Although I am not new to Python, this is my first attempt at using
Although I know how to build a DOM the long, arduous way using the
Although this has been discussed many times here in Stackoverflow, I couldn't get the

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.