1

so I know how I can iterate over and make array within hash

 travel=["Round Trip Ticket Price:", "Price of Accommodation:", "Number of checked bags:"] (1..3).each_with_object({}) do |trip, travels| puts "Please input the following for trip # #{trip}" travels["trip #{trip}"]= travel.map { |q| print q; gets.chomp.to_f } end ==>{"trip 1"=>[100.0, 50.0, 1.0], "trip 2"=>[200.0, 100.0, 2.0], "trip 3"=>[300.0, 150.0, 3.0]} 

BUT instead I want to iterate over to make three individual hashes within one array. I want it to look something like this

 travels=[{trip_transportation: 100.0, trip_accommodation:50.0, trip_bags:50} {trip_transportation:200.0, trip_accommodation:100.0, trip_2_bags:100} {trip_3_transportation:300.0, trip_accommodation:150.0, trip_3_bags:150}] 

I am really confused, basically the only thing I want to know how to do is how do I make three separate hashes while using a loop. I want every hash to represent a trip. Is that even possible?

1
  • Hashes are very versatile but for static structures, you might want to create a Travel or Trip class. Commented Oct 24, 2019 at 11:39

2 Answers 2

2
travel=[{ prompt: "Round Trip Ticket Price: ", key: :trip_transportation, type: :float }, { prompt: "Price of Accommodation : ", key: :trip_accommodation, type: :float }, { prompt: "Number of checked bags : ", key: :trip_bags, type: :int }] nbr_trips = 3 

Suppose that as the following code is run the user were to input the values given in the question's example.

(1..nbr_trips).map do |trip| puts "Please input the following for trip #{trip}" travel.map do |h| print h[:prompt] s = gets [h[:key], h[:type] == :float ? s.to_f : s.to_i] end.to_h end #=> [{:trip_transportation=>100.0, :trip_accommodation=>50.0, :trip_bags=>1}, # {:trip_transportation=>200.0, :trip_accommodation=>100.0, :trip_bags=>2}, # {:trip_transportation=>300.0, :trip_accommodation=>150.0, :trip_bags=>3}] 

I see no reason for keys to have different names for different trips (e.g., :trip_2_bags and trip_3_bags, rather than simply trip_bags for all trips).

Sign up to request clarification or add additional context in comments.

Comments

0

Using an Hash for setting up, similar to Cary Swoveland's answer and similar to my answer here: https://stackoverflow.com/a/58485997/5239030

travel = { trip_transportation: { question: 'Round Trip Ticket Price:', convert: 'to_f' }, trip_accommodation: { question: 'Price of Accommodation:', convert: 'to_f' }, trip_bags: { question: 'Number of checked bags:', convert: 'to_i' } } n = 2 res = (1..n).map do # |n| # uncomment if (*) travel.map.with_object({}) do |(k, v), h| puts v[:question] # k = k.to_s.split('_').insert(1, n).join('_').to_sym # uncomment if (*) h[k] = gets.send(v[:convert]) end end res #=> [{:trip_transportation=>10.0, :trip_accommodation=>11.0, :trip_bags=>1}, {:trip_transportation=>20.0, :trip_accommodation=>22.0, :trip_bags=>2}] 


(*) Uncomment if you want the result to appear like:

#=> [{:trip_1_transportation=>10.0, :trip_1_accommodation=>11.0, :trip_1_bags=>1}, {:trip_2_transportation=>20.0, :trip_2_accommodation=>22.0, :trip_2_bags=>2}] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.