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.
#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
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
In ruby, indentation is NG with the tab key!
Make an indent with the space key!
list = [1, 2, 3, 4, 5]
for item in list
puts item
end
#Execution result
1
2
3
4
5
a = 1
while a <= 10 do
puts a
a += 1
end
#Execution result
1
2
3
4
5
6
7
8
9
10
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
def drinkServer(fruit)
drink = fruit + 'juice'
return drink
end
puts drinkServer('Apple')
#Output result
Apple juice
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
class class name<Class name you want to inherit
end
Introduction to Tohoho's Ruby-Introduction to Tohoho's WWW
Recommended Posts