Maintenant que j'étudie le rubis sur rails, je vais écrire la syntaxe de base pour que je puisse la comprendre.
Qu'est-ce que le rubis?
Une langue caractérisée par. (Je pense qu'il y en a beaucoup d'autres)
count = 10
hello = "Hello"
puts count #42
puts hello #"Hello"
puts "#{hello} #{42}tokyo" #Hello 42tokyo
#Tableau
colors = [red, blue, green, pink, white]
#hacher
#Les trois lignes ci-dessous sont identiques
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:) #Voici la variable de l'instance
self.name = name
self.price = price
end
def info
return "#{self.name} #{self.price}Cercle"
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 yens
1 :pomme 120 yens
2 :banane 100 yens
3 :citron 80 yens
====================
choose menu :
3
selected menu : lemon
how many?
20
your check is 1500
Recommended Posts