ruby basic syntax memo

Introduction

Now that I'm studying ruby on rails, I'll write down the basic syntax so that I can understand it.

What is ruby

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)

Basic syntax

output


count = 10
hello = "Hello"

puts count #42
puts hello #"Hello"

puts "#{hello} #{42}tokyo"  #Hello 42tokyo

Hashes and arrays

#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

Method (function)

def hello
    puts "Hello World"
end

def hello(num, name)
   puts "Hello #{num}#{name}"
end

#hello(42, tokyo) -> Hello 42tokyo

Food ordering system


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

ruby basic syntax memo
Ruby memo
Ruby syntax summary
[wip] Ruby memo
Kotlin's simple basic syntax
Ruby learning points (basic)
java basic knowledge memo
Java learning memo (basic)
ruby memorandum (basic edition)
Basic methods of Ruby hashes
Java basic syntax + α trap
Basic methods of Ruby arrays
Ruby study memo (conditional branching)
[Self-use memo] (Ruby) class, class instance, instance
Personal memo Progate Ruby I (2)
Ruby syntax errors and countermeasures
Ruby on Rails basic learning ①
[Ruby] List of basic commands
Ruby study memo (recursive function)
Personal memo Progate Ruby I (1)
Ruby methods and classes (basic)
Ruby on Rails Basic Memorandum
ruby exercise memo I (puts)
[Ruby ~ Iterative processing ~] Study memo 4
Review of Ruby basic grammar
Ruby design pattern template method pattern memo
[Devise] rails memo basic setting initial setting
Basic knowledge of Ruby on Rails
Ruby Study Memo (Test Driven Development)
Ruby on Rails 6.0 environment construction memo
String output method memo in Ruby
Memorandum (Ruby: Basic grammar: Iterative processing)
Spring Security usage memo Basic / mechanism
[Note] [Beginner] Ruby writing memo (refactoring) 1
With ruby ● × Game and Othello (basic review)
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Technical memo] What is "include" in Ruby?
Introduction to Ruby basic grammar with yakiniku
part of the syntax of ruby ​​on rails
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
Memorandum (Ruby: Basic Grammar: Classes and Instances)
ruby Exercise Memo II (variable and method)
Ruby on Rails6 Practical Guide cp4 ~ cp6 [Memo]
Basic cheat sheet by language (Ruby, PHP)
Ruby on Rails6 Practical Guide cp10 ~ cp12 [Memo]
Ruby on Rails6 Practical Guide cp16 ~ cp18 [Memo]