Sorting and loops in ruby

JavaCid

New Member
#1
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
Mã:
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
 

Admin

Administrator
Thành viên BQT
#2
You should not wrap the code in class in the first place. There is no OOP in your code, hence the class is not needed as well. Also, gets returns a string, while for number you likely need an integer.

Here would be a [more-or-less] rubyish version of your code:

Mã:
print "How many gemstones do you want to enter? "
#                     ⇓⇓⇓⇓⇓ get rid of trailing CR/LF
#                           ⇓⇓⇓⇓ convert to integer
gemstoneNumber = gets.chomp.to_i

gemstones =
  1.upto(gemstoneNumber).map do |i|
    puts
    puts "Please enter data for the gemstone ##{i}:"

    print "What is the name of the gemstone? "
    name = gets.chomp # shave the trailing CR/LF off
    print "What is the color of the gemstone? "
    color = gets.chomp
    print "What is the price of the gemstone? "
    price = gets.chomp.to_f # convert to float

    # in Ruby we normally use hashes to store
    #   the named values
    {name: name, color: color, price: price}
  end

puts "You entered #{gemstoneNumber} gemstones. They are:"
gemstones.each do |gemstone|
  puts "Name: #{gemstone[:name]}. " \
       "Color: #{gemstone[:color]}. " \
       "Price: $#{gemstone[:price]}."
end
Alternatively, you might use the class instead of hash to store the gemstone info.

To sort the gemstones by the name:

Mã:
puts "You entered #{gemstoneNumber} gemstones. They are:"
#         ⇓⇓⇓⇓⇓⇓⇓ HERE
gemstones.sort_by { |gemstone| gemstone[:name] }.each do |gemstone|
  puts "Name: #{gemstone[:name]}. " \
       "Color: #{gemstone[:color]}. " \
       "Price: $#{gemstone[:price]}."
end
The good documentation on enumerations might be found in the official ruby docs: https://ruby-doc.org/core/Enumerable.html#method-i-sort_by (and around.)
 
OP
OP
J

JavaCid

New Member
#3
what if I wanted to use a custom module to use in the class. How would I go about doing it?
 

Admin

Administrator
Thành viên BQT
#4
I am not sure I follow. Normally you define a module with module Foo; ...; end and use it by mixing it in with include, prepend or extend.
 
OP
OP
J

JavaCid

New Member
#5
What I mean is I would like to create a module and use it in the class. I made a mistake in the earlier comment. Sorry
 

Admin

Administrator
Thành viên BQT
#6
Then my previous comment applies. Create a module and include it into the class.
 
Top