0

I've been looking for an answer to this question for a while, but I haven't been able to find one that I was able to understand and apply.

I have a class that contains three instance variables: @brand, @setup, and @year. I have a module, included in that class, that has three methods: print_brand(), print_setup(), and print_year() that simply print the value assigned to the associated variable.

I'd like to get two strings from a user and use the first as an object name and the second as a method name. Here's what I have right now:

class Bike
  include(Printers)
  def initialize(name, options = {})
    @name = name
    @brand = options[:brand]
    @setup = options[:setup]
    @year = options[:year]
  end
end

trance = Bike.new("trance x3", {
    :brand => "giant",
    :setup => "full sus",
    :year => 2011
    }
  )
giro = Bike.new("giro", {
    :brand => "bianchi",
    :setup => "road",
    :year => 2006
    }
)
b2 = Bike.new("b2", {
    :brand => "felt",
    :setup => "tri",
    :year => 2009
    }
)

puts "Which bike do you want information on?"
b = gets()
b.chomp!

puts "What information are you looking for?"
i = gets()
i.chomp!

b.send(i)

I'm missing some functionality that converts b from a string to an object name. For example, I want the user to be able to type in "trance" and then "print_year" and have "2011" printed on the screen. I tried to use constantize on b, but that doesn't seem to work. I get the error:

in 'const_defined?': wrong constant name trance (NameError)

Any other ideas?

2
  • If you say b.send(i), that assumes that b is an object that has whatever i is set to as a method. If b is a specific bike type, where's the object whose name is that bike type? Your bike types are variables that are instances of Bike. This seems like a complex way of providing data to a user based upon a query. Commented Jun 23, 2013 at 22:51
  • I'd recommend using a database rather than try to store the settings/options in RAM. Modifying code to add a new bike is not a good idea; Instead you should separate your code and data. Look at a disk-based SQLite DB with something like Sequel used to access it. Commented Jun 24, 2013 at 1:12

2 Answers 2

1

You should store the object in a hashmap with key = name and value = object, then use b (name) to retrieve the right object from the hashmap. I'm still not sure what do you want to do with the second input, by my guess is that this answer covers that as well.

h = Hash.new()
h["trance x3"] = trance 
h["giro"] = giro 
...
puts "Which bike do you want information on?"
b = gets()
b.chomp!
user_bike = h[b]

puts "What information are you looking for?"
i = gets()
i.chomp!

user_bike.send(i)
Sign up to request clarification or add additional context in comments.

Comments

1

I would use eval:

eval "#{ b }.#{ i }"

I suppose you have to add accessors:

attr_accessor :brand, :setup, :year

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.