Use hashes well in Ruby to calculate the total amount of an order

This article uses Ruby 2.6.3 installed on macOS.

I made a note of how to solve the Ruby super-introduction exercise (p176) that can be understood from scratch. At first, I couldn't solve it this way because I thought that the key was written as a symbol. I felt that I needed the ability to determine where to use hashes.

Q: There are two menus, coffee 300 yen and cafe latte 400 yen. The size is +0 yen for short, +50 yen for tall, and +100 yen for venti. Take an order using the method.

Version that can be ordered from the terminal


def price
  puts "What would you like to order?"
  item = gets.chomp
  puts "What size do you want?"
  size = gets.chomp
  items = {"coffee" => 300, "Latte" => 400}
  sizes = {"short" => 0, "Thor" => 50, "Venti" => 100}
  total = items[item] + sizes[size]
  puts "#{total}It will be a circle"
end

price

How to use keyword arguments and default values ​​(almost the same as the answer in the book)


def price(item: "coffee", size: "Thor")
  items = {"coffee" => 300, "Latte" => 400}
  sizes = {"short" => 0, "Thor" => 50, "Venti" => 100}
  items[item] + sizes[size]
end

puts price(item: "Latte", size: "Venti")

Recommended Posts

Use hashes well in Ruby to calculate the total amount of an order
I tried to make full use of the CPU core in Ruby
[Ruby] How to calculate the total amount using the initialize method and class variables
How to use git with the power of jgit in an environment without git commands
How to retrieve the hash value in an array in Ruby
[Ruby] I want to reverse the order of the hash table
Order of processing in the program
I want to change the value of Attribute in Selenium of Ruby
How to get the length of an audio file in java
In order not to confuse the understanding of getters and setters, [Do not use accessors for anything! ]
Summary of hashes and symbols in Ruby
[Ruby] I want to display posted items in order of newest date
How to find the total number of pages when paging in Java
In the topic of "total concentration", "How to use Docker" is summarized for the virtual Mameko who sleeps in me.
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
[Swift] How to get the number of elements in an array (super basic)
I get an error when I try to use "^" or "$" in ruby ​​regular expression
[Ruby] Learn how to use odd? Even? And count the even and odd numbers in the array!
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
[Order method] Set the order of data in Rails
How to find the cause of the Ruby error
I want to use arrow notation in Ruby
The story of introducing Ajax communication to ruby
Output of how to use the slice method
[Ruby] Code to display the day of the week
How to use JQuery in js.erb of Rails6
How to build the simplest blockchain in Ruby
I want to get the value in Ruby
[Ruby basics] How to use the slice method
[Ruby] How to use the map method. How to process the value of an object and get it by hash or symbol.
What to do if you can't get the text of an element in Selenium
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
For those who want to use MySQL for the database in the environment construction of Rails6 ~.
Offline real-time how to write Implementation example of the problem in E05 (ruby, C11)
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
How to request by passing an array to the query with HTTP Client of Ruby
Summary of how to use the proxy set in IE when connecting with Java
Count the number of occurrences of a string in Ruby
I want to use Combine in UIKit as well.
[Ruby] How to use standard output in conditional branching
In Time.strptime,% j (total date of the year) is
[Ruby] The role of subscripts in learning elements in arrays
[Ruby] How to find the sum of each digit
Understand the characteristics of Scala in 5 minutes (Introduction to Scala)
Calculate the difference between numbers in a Ruby array
The story of an Illegal State Exception in Jetty.
Mandels to create an image of the Mandelbrot set
How to use CommandLineRunner in Spring Batch of Spring Boot
[Swift] Use UserDefaults to save data in the app
Java: Use Stream to sort the contents of the collection
Get the URL of the HTTP redirect destination in Ruby
[Rails] How to display an image in the view
Handling of date and time in Ruby. Use Date and Time properly.
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
[Ruby] Conditional expression without if? Meaning of (question mark). How to use the ternary operator.
I tried to solve the Ruby karaoke machine problem (there is an example of the answer)
How to specify an array in the return value / argument of the method in the CORBA IDL file
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
Display a balloon message in BarButtonItem of NavigationBar with a size according to the amount of text.
How to use hashes
How to use the getter / setter method (in object orientation)