Now that I'm studying ruby on rails, I'll write down the basic syntax so that I can understand it.
What is ruby?
--Interpreter language (<-> compiler language) --Dynamic typed language (<-> statically typed language) --Object-oriented (<->)
Is a characteristic language. (I think there are many others)
count = 10
hello = "Hello"
puts count #42
puts hello #"Hello"
puts "#{hello} #{42}tokyo" #Hello 42tokyo
#Array
colors = [red, blue, green, pink, white]
#hash
#All three lines below are the same
exam1 = {"subject" => "math", "score" => 100}
exam1 = {:subject=> "math", :score => 100}
exam1 = {subject: "math", score: 100}
exams = [
{subject: "math", score: 100},
{subject: "english", score: 40},
{subject: "japanese", score: 60},
{subject: "science", score: 90}
]
for, while
for i in [0,1,2,3] do
puts i
end
index = 0
while index <= 10 do
puts index
end
each
colors = [red, blue, green, pink, white]
colors.each do |color|
puts color
end
def hello
puts "Hello World"
end
def hello(num, name)
puts "Hello #{num}#{name}"
end
#hello(42, tokyo) -> Hello 42tokyo
require "./menu"
menu1 = Menu.new(name: "sushi", price: 1000)
menu2 = Menu.new(name: "apple", price: 120)
menu3 = Menu.new(name: "banana", price: 100)
menu4 = Menu.new(name: "lemon", price: 80)
menus = [menu1, menu2, menu3, menu4]
puts "=== this is menu ==="
index = 0
menus.each do |menu|
puts "#{index} : #{menu.info}"
index += 1
end
puts "===================="
puts "choose menu : "
order = gets.chomp.to_i
selected_menu = menus[order]
puts "selected menu : #{selected_menu.name}"
puts "how many?"
count = gets.chomp.to_i
puts "your check is #{selected_menu.get_total_price(count)}"
class Menu
attr_accessor :name
attr_accessor :price
def initialize(name:, price:) #Here is the instance variable
self.name = name
self.price = price
end
def info
return "#{self.name} #{self.price}Circle"
end
def get_total_price(count)
total_price = self.price * count
if count >= 3
total_price -= 100
end
return total_price
end
end
=== this is menu ===
0 :sushi 1000 yen
1 :apple 120 yen
2 :banana 100 yen
3 :lemon 80 yen
====================
choose menu :
3
selected menu : lemon
how many?
20
your check is 1500
Recommended Posts