Ruby Learning # 11 Building a Calculator

puts "Enter a number: " num1 = gets.chomp() puts "Enter another number" num2 = gets.chomp()

puts (num1 + num2)

Enter a number 5

Enter another number 2 52 Since it is a String, it cannot be added. Use to_i for resolution

puts "Enter a number: " num1 = gets.chomp() puts "Enter another number" num2 = gets.chomp()

puts (num1.to_i + num2.to_i)

Enter a number 5

Enter another number 2 7

Enter a number 5

Enter another number 2.5 7

Does not recognize after the decimal point. Use to_f for resolution puts (num1.to_f + num2.to_f)

Enter a number 5

Enter another number 2.5 7.5 The following is the cleaner source puts "Enter a number: " num1 = gets.chomp().to_f puts "Enter another number" num2 = gets.chomp().to_f

puts (num1 + num2)

Enter a number 5.5

Enter another number 2.1 7.6

Recommended Posts

Ruby Learning # 11 Building a Calculator
Ruby Learning # 32 Building a Quiz
Ruby Learning # 22 Building a Guessing Game
Ruby Learning # 12 Building a Mad Libs Game
Ruby Learning # 2 Drawing a Shape
Ruby learning 4
Ruby learning 5
Ruby learning 3
Ruby learning 2
ruby calculator
Ruby learning 6
Ruby learning 1
Building a Deep Learning environment (Ubuntu 20.04 LTS)
Ruby Learning # 25 Comments
Ruby Learning # 13 Arrays
Ruby Learning # 1 Introduction
Ruby Learning # 34 Modules
Ruby Learning # 14 Hashes
[Ruby] Building a Ruby development environment on Ubuntu
Ruby Learning # 33 Inheritance
Ruby Learning # 15 Methods
Building a Ruby environment for classes on Mac
Ruby Learning # 30 Initialize Method
Ruby learning points (basic)
Ruby Learning # 29 Classes & Objects
Ruby Learning # 20 Case Expressions
Ruby Learning # 24 Exponent Method
Ruby Learning # 17 If Statements
Ruby Learning # 21 While Loops
Ruby Learning # 31 Object Methods
Ruby Learning # 27 Writing Files
Ruby Learning # 35 Interactive Ruby (irb)
Ruby Learning # 9 Math & Numbers
Ruby Learning # 28 Handling Errors
Ruby Learning # 26 Reading Files
Ruby Learning # 23 For Loops
Ruby Learning # 16 Return Statement
[Ruby] Extracting a two-dimensional array
Ruby on rails learning record -2020.10.03
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
What is a Ruby module?
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Creating a calendar using Ruby
Ruby on Rails basic learning ①
Ruby Learning # 99 Personal Programming Notebook
Ruby on rails learning record-2020.10.07 ②
Multiplication in a Ruby array
Learning Ruby with AtCoder 13 How to make a two-dimensional array
Ruby Learning # 10 Getting User Input
Create a fortune using Ruby
Ruby Learning # 8 Working With String
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
Ruby ① Build a Windows environment
Cut out a Ruby string
Sorting hashes in a Ruby array
Completed Progate Ruby Learning Course III
Ruby: I made a FizzBuzz program!
Make a language! (Making a simple calculator ②)
Learning Ruby with AtCoder 6 [Contest 168 Therefore]