I tried to summarize the basic grammar of Ruby briefly

I have extracted and summarized the parts that I personally do not understand. Please see the details from the link at the bottom of the page.

hash

#key:Store value in a lump
fruits = {"a":"apple", "b":"grape", "c":"orange"} 
puts fruits
puts fruits[:a] #Extract by key value

#Execution result
{:a=>"apple", :b=>"grape", :c=>"orange"}
apple

Conditional branch

if condition A
Processing when the conditions are met
elsif condition B
Processing when condition B is not matched and condition A is not matched
else
Condition A,Processing when neither of B matches
end

Indent

In ruby, indentation is NG with the tab key!

Make an indent with the space key!

Iterative processing

for statement

list = [1, 2, 3, 4, 5]
for item in list
    puts item
end

#Execution result
1
2
3
4
5

while statement

a = 1
while a <= 10 do
    puts a
    a += 1
end

#Execution result
1
2
3
4
5
6
7
8
9
10

Exception handling

begin

begin
Code to execute
rescue
Code that runs only when an exception occurs
else
Code that runs only when no exception is raised
end

rescue

begin
Code to execute
rescue
Code that runs only when an exception occurs
else
Code that runs only when no exception is raised
ensure
Last code executed with or without exceptions
end

raise

raise error type

Method

#Method
def drinkServer(fruit)
    drink = fruit + 'juice'
    return drink
end

puts drinkServer('Apple')

#Output result
Apple juice

class

A group of entire processes such as methods

#Declare a class to handle circles
class Circle #Uppercase letters at the beginning of the class name
 
  def area_circle
    puts @radius * @radius * 3.14
  end
 
  #When functions etc. are continuous, insert one blank line between them.
  def circumference
    puts @radius * 2 * 3.14
  end
 
  def radius=(radius)
    @radius = radius
  end
end
 
#Create an instance
circle1 = Circle.new
 
#Enter the radius
circle1.radius = 3
 
#Call a function
circle1.area_circle
circle1.circumference

Inheritance class

class class name<Class name you want to inherit
 
end

Reference article

A thorough guide to Ruby for beginners [Explanation from the basics in an easy-to-understand manner] | Samurai Blog --Site for programming beginners )

Introduction to Tohoho's Ruby-Introduction to Tohoho's WWW

Ruby to start in 20 minutes

Recommended Posts

I tried to summarize the basic grammar of Ruby briefly
I tried to summarize the state transition of docker
I tried to summarize the basics of kotlin and java
I tried to summarize the methods used
I tried to summarize the Stream API
I tried to solve the problem of "multi-stage selection" with Ruby
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
I tried to summarize the methods of Java String and StringBuilder
I tried to chew C # (basic of encapsulation)
I tried to summarize the key points of gRPC design and development
I tried to make full use of the CPU core in Ruby
[Ruby] I tried to summarize the methods that frequently appear in paiza
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
Review of Ruby basic grammar
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
Java engineers now compare to learn the basic grammar of Ruby Part 1 (Basic, Variables)
Java engineers now compare to learn the basic grammar of Ruby Part 2 (classes, methods)
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
I tried to summarize iOS 14 support
I tried to explain the method
I tried to build the environment of WSL2 + Docker + VSCode
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
[Ruby] I want to reverse the order of the hash table
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View
I tried to summarize what was asked at the site-java edition-
I tried to build the environment of PlantUML Server with Docker
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
Special Lecture on Multi-Scale Simulation: I tried to summarize the 8th
I tried to check the operation of gRPC server with grpcurl
I want to change the value of Attribute in Selenium of Ruby
Special Lecture on Multi-Scale Simulation: I tried to summarize the 7th
I tried to solve the problem of Google Tech Dev Guide
I tried to summarize Java lambda expressions
I tried to implement the Iterator pattern
[Java] I personally summarized the basic grammar.
What is Docker? I tried to summarize
I tried to build Ruby 3.0.0 from source
I tried to solve the Ruby bingo card creation problem (there is an example of the answer)
I tried to make a parent class of a value object in Ruby
I tried to summarize the stumbling points when developing an Android application
[Ruby] I want to extract only the value of the hash and only the key
[Introduction to Java] I tried to summarize the knowledge that I think is essential
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
I tried to visualize the access of Lambda → Athena with AWS X-Ray
I translated the grammar of R and Java [Updated from time to time]
I tried to measure and compare the speed of GraalVM with JMH
I tried to summarize about JVM / garbage collection
I want to output the day of the week
[Rails] I tried to summarize the passion and functions of the beginners who created the share house search site!
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
The story of introducing Ajax communication to ruby
I want to var_dump the contents of the intent
[Must see !!!] I tried to summarize object orientation!
[Ruby] Code to display the day of the week
[Ruby basics] I tried to learn modules (Chapter 1)
I tried to set tomcat to run the Servlet.
I tried using the profiler of IntelliJ IDEA