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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:59:11+00:00 2026-05-25T02:59:11+00:00

Total Ruby noob. I’m going through Zed Shaw’s LRTHW, and I’m stuck on the

  • 0

Total Ruby noob. I’m going through Zed Shaw’s LRTHW, and I’m stuck on the Hash exercise. I can’t make any sense of this code, and can’t find anything on the web that looks like it.

cities = {'CA' => 'San Francisco', 'MI' => 'Detroit', 'FL' => 'Jacksonville'}

cities['NY'] = 'New York'
cities['OR'] = 'Portland'

def find_city (map, state)
  if map.include? state
    return map[state]
  else
    return "Not found."
  end
end

cities[:find] = method(:find_city)

while true
  print "State? (Enter to quit) "
  state = gets.chomp

  break if state.empty?

  puts cities[:find].call(cities, state)
end

Basically, I’m just stuck. I can’t make the connection between how I type in a state and it returns the city. Any dumbed down explanation would be greatly appreciated.

  • 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-25T02:59:11+00:00Added an answer on May 25, 2026 at 2:59 am

    Here there are a hash (cities) with 3 pairs key => value

    cities = {'CA' => 'San Francisco', 'MI' => 'Detroit', 'FL' => 'Jacksonville'}
    

    You can access that values by the key:

    puts cities['CA']  #=>  'San Francisco'
    

    Now we add two new pairs.

    cities['NY'] = 'New York'
    cities['OR'] = 'Portland'
    

    The whole hash will be:

    p cities #=> {'CA' => 'San Francisco', 'MI' => 'Detroit', 'FL' => 'Jacksonville', 'NY' => 'New York', 'OR' => 'Portland'}
    

    Now it’s defined a method called find_city. It takes a hash and a key.

    def find_city (map, state)
      # if hash has the key, return its value. (There are better ways to do it.)
      if map.include? state
        return map[state]
      else
        return "Not found."
      end
    end
    

    Here there is the worst piece of Ruby code I’ve ever seen.

    cities[:find] = method(:find_city)
    

    Ok, that code gets the method find_city and turns it into a Method object, which could be assigned to a variable. BUT, instead of using a normal local variable, it is stored in a value of the cities hash!

    That object is similar to a method, but it’s an object instead of a method, and it must be called with call (in Ruby 1.8.7).

    I will use a variable called my_meth (it could be any name) to explain better.

    # get the method find_city and turns it into an object assigned to my_meth
    my_meth = method(:find_city)
    
    while true
      print "State? (Enter to quit) "
      state = gets.chomp
    
      break if state.empty?
    
      # here we use find_city on the cities hash.
      puts my_meth.call(cities, state)
    end
    
    puts my_meth.class
    

    But, instead to use a variable, the original code stored the Method object on the cities hash. So, cities will be:

    cities[:find] = method(:find_city)
    p cities #=> {'CA' => 'San Francisco', 'MI' => 'Detroit', 'FL' => 'Jacksonville', 'NY' => 'New York', 'OR' => 'Portland', :find => (the Method object)}
    

    So, you can access the find_city thru cities[:find].call.

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

Sidebar

Related Questions

First of all you have to know I'm total Ruby noob :) I installed
I'm new to ruby. Is there any way how can I shorten this code?
I'm a total Ruby/Rails/AR noob. I have a very basic sort of database schema
Total noob to anything lower-level than Java, diving into iPhone audio, and realing from
Total noob question, but here. CSS .product__specfield_8_arrow { /*background-image:url(../../upload/orng_bg_arrow.png); background-repeat:no-repeat;*/ background-color:#fc0; width:50px !important; height:33px
total n00b question here but I can't seem to find the answer searching around
I am total begineer in ruby so its very novice question. I am trying
In Ruby, I have a hash of objects. Each object has a type and
I'm new to Rails (and ruby). What is the standard way of iterating through
In Ruby, how can I do: number_total = records / per_page where per_page =

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.