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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:15:58+00:00 2026-06-13T08:15:58+00:00

Given a class instance and a string, how do I convert the string to

  • 0

Given a class instance and a string, how do I convert the string to refer to the instance?

class Room
  def enter
    puts "Welcome!"
  end
end

# Rooms are predefined
lounge = Room.new
kitchen = Room.new
study = Room.new

puts "Which room would you like to go to?"
print "> "
room = gets.strip

# User types "lounge"

room.enter # => undefined method `enter' for "lounge":String (NoMethodError)

I understand why I’m getting NoMethodError, but I haven’t been able to work out how to convert the room string to refer to the existing instance of Room named lounge.

  • 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-13T08:16:00+00:00Added an answer on June 13, 2026 at 8:16 am

    I ended up storing each Room object in a hash as a class variable on initialization. This lets you refer to each instance via the hash:

    class Room
      @@rooms = {}
    
      def initialize(name)
        @@rooms.store name, self
      end
    
      def rooms
        @@rooms
      end
    
      def enter
        puts "Welcome!"
      end
    end
    
    # Rooms are predefined
    lounge = Room.new('lounge')
    kitchen = Room.new('kitchen')
    study = Room.new('study')
    
    puts "Which room would you like to go to?"
    print "> "
    new_room = gets.strip
    
    lounge.rooms[new_room].enter if lounge.rooms.has_key? new_room
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Observer def initialize(&block) instance_eval(&block) if block_given? end end I'm wondering what assumption is
I have method that returns module path of given class name def findModulePath(path, className):
Given a class name as a string, how do I get the package name
Given a custom, new-style python class instance, what is a good way to hash
Given an instance of the class ThisClassShouldBeTheDataContext as the Datacontext for the view class
Given: class StringRecord : INotifyPropertyChanged { public string Key { get; set; } //
Foo = Class.new Foo.class_eval do def class_bar class_bar end end Foo.instance_eval do def instance_bar
I want to flatten/transform an instance from a given Java class to another instance
Given the class: public class Foo { public string Name { get; set; }
In Clojure, given a class name as a string, I need to create a

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.