A list of basic ruby items for memorandum.
If you remember puts, hashes, symbols, how to write each method, and elsif of if expression, the rest is almost the same as other languages.
table of contents
puts "AAA"
└ "'" is OK
└ Half-width space required after puts
#
puts 'AAA' + 'BBB'
└ Numerical values and character strings cannot be concatenated
└ Unify types (or variable expansion)
Variable name = number
Variable name ='string'
x=x+10
→ x+=10
x=x-10
→ x-=10
x=x*10
→ x*=10
x=x/10
→ x/=10
x=x%10
→ x%=10
puts "#{ }"
└ Double quotation
└ * Single quotation is output as a character string as it is
python
hello = "Hello"
name = "AAA"
puts "#{hello}、#{name}Mr."
Variable expansion of python and php. In ruby, it is called expression expansion.
python
if conditional expression 1
processing
elsif conditional expression 2
processing
else
processing
end
└ elsif
(python:elif, php: else if)
└ Conditional expression is replaced with true / false
└ If false, skip processing
==
equal
! =
Not equal
Greater than >
Smaller than <
> =
or more
<=
Below
&&
and
||
Or
[Element 1, Element 2, Element 3, ,,,]
python
array = ["AAA","BBB","CCC"]
puts array[0]
puts "The element of index number 1 is#{array[1]}is."
#output
AAA
The element with index number 1 is BBB.
python
Array.each do |Variable name|
processing
end
└ .each do| |
└ Finally ʻend` required
└ The defined variable name can be used only in each method.
Example of use
elements = ["AAA","BBB","CCC"]
elements.each do |element|
puts "The contents of the array#{element}is."
#output
The contents of the array are AAA.
The contents of the array is BBB.
The contents of the array are CCC.
: Value
└ Symbol type
└ Can be used as a substitute for character strings
└ Often used as a hash key
python
puts "AAA"
puts :AAA
#output
AAA
AAA
The name of the array with each value given a name (key).
{Key 1 => Value 1, Key 2 => Value 2, Key 3 => Value 3, ,,,}
└ Enclose the character string in "" "(key, value)
└ Braces: {}
└ Assign a hash to the array
Hash example
elements = {"key1"=>"A","key2"=>"B", "key3"=>"C"}
puts elements["key2"]
#output
B
** ▼ Other languages ** ・ Python dictionary type
{Key 1: Value 1, Key 2: Value 2, Key 3: Value 3, ,,,}
・ Php associative array(Key 1 => Value 1, Key 2 => Value 2, Key 3 => Value 3, ,,,);
Hash ["key name "] = value you want to change
Hash example
elements = {"key1"=>"A","key2"=>"B", "key3"=>"C"}
elements["key2"]=222
puts elements["key2"]
#output
222
python
elements = {"key1"=>"A","key2"=>"B", "key3"=>"C"}
elements["key4"]="D"
puts elements
#output
{"key1"=>"A","key2"=>"B", "key3"=>"C", "key4"=>"D"}
The key name can be represented by adding ":" instead of a character string.
: Key name => value
└ Acquisition array [: key name]
python
elements = {:key1=>"A",:key2=>"B", :key3=>"C"}
puts elements[:key2]
#output
B
: Key name => value
→ key name: value
└ * Use symbols when acquiring
└ Acquisition array [: key name]
Symbol (abbreviation)
elements = {key1:"A",key2:"B", key3:"C"}
puts elements[:key2]
#output
B
nil
nil
└ Empty elements
└ When nil itself is output, it becomes an empty string
--If you specify a key that does not exist, the value will be nil. --No error
nil
elements = {key1:"A",key2:"B", key3:"C"}
puts elements[:key4]
puts nil
#output
In addition to true and false conditional expressions, if expressions can use nil or others.
・ Nil → false ・ Other than nil → true
python
elements = {key1:"A",key2:"B", key3:"C"}
if elements[:key4]
puts "The value of key4 is#{elements[:key4]}is"
else
puts "There is no key4"
end
#output
There is no key4
You can put hashes in the elements of the array.
python
elements = [{order:"A",number:1}, {order:"B",number:2}, {order:"C",number:3}]
Arrange with line breaks
elements = [
{order:"A",number:1},
{order:"B",number:2},
{order:"C",number:3}
]
puts elements[0]
#output
{order:"A",number:1}
python
elements = [
{order:"A",number:1},
{order:"B",number:2},
{order:"C",number:3}
]
#Assign to a variable and retrieve
element = elements[0]
puts element[:order]
#Extract in one line
puts elements[0][:order]
#output
A
A
each
Array.each do |Variable name|
processing
end
each and hash
elements = [
{order:"A",number:1},
{order:"B",number:2},
{order:"C",number:3}
]
elements.each do |element|
puts "#{element[:order]}The numbers are#{element[:number]}is"
end
#output
The number of A is 1
B number is 2
The number of C is 3
python
elements = [
{order:"A",number:1},
{order:"B"},
{order:"C",number:3},
{order:"D"}
]
elements.each do |element|
#Determine if there are numbers
if element[:number]
puts "#{element[:order]}The numbers are#{element[:number]}is"
else
puts "#{element[:order]}There are no numbers"
end
end
#output
The number of A is 1
There is no B number
The number of C is 3
There is no D number
Recommended Posts