ruby on rails
ruby 기본 입출력
흰두부1
2020. 11. 7. 17:11
# To start irb console: irb
참고: www.garethrees.co.uk/2013/05/04/p-vs-puts-vs-print-in-ruby/
Ruby p vs puts vs print | Gareth Rees: Ruby Programmer and Product Manager from Cardiff, Wales UK
Ruby p vs puts vs print Ruby has three useful methods for printing information to the command line: Kernel#p, Kernel#print and Kernel#puts. Fire up irb and follow along! > class P > def inspect(); "P#inspect"; end > def to_s(); "P#to_s"; end > end > q = P.
www.garethrees.co.uk
# string 안에서 변수 사용: #{}
first_name = "Mashrur"
last_name = "Hossain"
full_name = "#{first_name} #{last_name}"
puts "My first name is #{first_name} and my last name is #{last_name}"
# 각 객체마다 method를 가지고 있음
puts full_name.class
puts full_name.methods
# nil vs empty
# nil : 객체인지?
{}.nil?
{}.empty?
# 입력: gets.chomp
# chomp: 입력 끝에 엔터 무시
# 예제
puts Enter your first name
first_name = gets.chomp
puts "your first name is #{first_name}"
puts Enter your number
number = gets.chomp
puts numer.to_i * 2
# 계산기 예제
puts "simple calculator"
20.times{print "-"}
puts
puts "first number"
num_1=gets.chomp
puts "second number"
num_2=gets.chomp
puts "first number + second number = #{num_1.to_f+num_2.to_f}"
puts "first number - second number = #{num_1.to_f-num_2.to_f}"
puts "first number * second number = #{num_1.to_f*num_2.to_f}"
puts "first number / second number = #{num_1.to_f/num_2.to_f}"
# 비교 연산자
# ==
# !=
# >
# <
puts 10.to_int == 10.to_f
puts 10.to_int === 10.to_f
puts 10.eql?(10.0)
# eql compare type