This is a program that ask input from the user about the characteristics of a gemstone then prints to the screen. Things like color, price and name. I have written it to an extend where the user enters all this and prints them out. Am now stuck where I should loop and enable the user to enter any number of gemstones. Like if he/she enters 3 gemstones then it should loop and allow the user to enter the characteristics of the 3 gemstone types. I would also like to sort the resulting output of gemstone names in alphabetical order. Appreciated

class GemStones
  # input variables
  name = ""
  color = ""
  price = 0
  gemstoneNumber = 0

  # output variable
  gemstoneNumber = 0

  # processing
  print "How many gemstones do you want to enter? "
  gemstoneNumber = gets

  print "What is the name of the gemstone? "
  name = gets

  print "What is the color of the gemstone? "
  color = gets

  print "What is the price of the gemstone? "
  price = gets

  puts " You entered #{gemstoneNumber} The name is #{name}, the color is #{color} and price is
  $ #{price}"

end

You have made a decent start. In addition to looping you need to think about how you might store the user-entered results.

In order to loop you're probably best using the #times method of an integer, this means you'll have to convert your gemstoneNumber to an integer before looping.

[1] pry(main)> quantity = "2".to_i    # simulating the gets which always retuns a string
=> 2
[2] pry(main)> quantity.times do |i|
[2] pry(main)*   puts i
[2] pry(main)* end
0
1

Secondly, how do you want to store your data? You could use a hash per gemstone and store the records in an array.

gemstones = []
gemstones.push({name: "Sapphire", color: "Blue", price: 24.0})
gemstones.push({name: "Emerald", color: "Green", price: 21.0})

Or you could create a new Gemstone class with attribute readers and store an array of these:

class Gemstone
  attr_reader :name, :colour, :price

  def initialize(name, colour, price)
    @name, @colour, @price = name, colour, price
  end

end

The beauty of this approach is that you can create a to_s method in your class and call it to print out your "You entered..." line.

Hopefully some of this made sense, but you can get the result you want by looping based on the quantity using times and storing the data in an array. If you get stuck post your updated code and I'll have another look. Good luck.

Additionally, I forgot the sorting element.

For an array of hashes you'll notice that if you try to sort directly the comparison will fail:

[{name: "Ringo"}, {name: "John"}, {name: "Paul"}, {name: "George"}].sort
ArgumentError: comparison of Hash with Hash failed

You need to tell Ruby how to sort, using #sort_by:

[{name: "Ringo"}, {name: "John"}, {name: "Paul"}, {name: "George"}].sort_by{|b| b[:name]}
=> [{:name=>"George"}, {:name=>"John"}, {:name=>"Paul"}, {:name=>"Ringo"}]

If you go down the class route you would need to implement the comparison method, <=>, as that's what is used to compare elements within sort. For the time being I'd stick to using a hash, it works nicely for this type of puzzle.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.