Jetzt, wo ich Ruby on Rails studiere, schreibe ich die grundlegende Syntax auf, damit ich sie verstehen kann.
Was ist Rubin?
--Interpreter-Sprache (<-> Compilersprache)
Eine Sprache, die gekennzeichnet ist durch. (Ich denke, es gibt viele andere)
count = 10
hello = "Hello"
puts count #42
puts hello #"Hello"
puts "#{hello} #{42}tokyo" #Hello 42tokyo
#Array
colors = [red, blue, green, pink, white]
#Hash
#Alle drei Zeilen unten sind gleich
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:) #Hier ist die Variable der Instanz
self.name = name
self.price = price
end
def info
return "#{self.name} #{self.price}Kreis"
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 :Apfel 120 Yen
2 :Banane 100 Yen
3 :Zitrone 80 Yen
====================
choose menu :
3
selected menu : lemon
how many?
20
your check is 1500
Recommended Posts