A list and examples of basic Ruby commands.
The difference between them is the presence or absence of line breaks and the presence or absence of type representation.
Method | puts | p | |
---|---|---|---|
Use | For production | For debugging | Remnants of python? |
new line | Yes | Yes | None |
Type representation | None | Yes | None |
Difference between puts and print (with or without line breaks)
puts "Hello"
puts 123
#Hello
#123
print "Hello"
print 123
#Hello 123
Difference between puts and p (with or without type representation)
puts "Hello"
puts 123
#Hello
#123
p "Hello"
p 123
#"Hello"
#123
TAX_RATE = 1.0
puts TAX_RATE
#Overwrite instruction
TAX_RATE = 1.5
#warning: already initialized constant TAX_RATE
#warning: previous definition of TAX_RATE was here
p 1.1.class
#Float
p 1.1.methods
#[:-@, :**, :<=>, :<=, :>=, :==, :===, :eql?, :%, :inspect, :*, :+, :-, :/, :<, :>, :to_int, :coerce, :to_s, :divmod, :to_i, :to_f, :to_r, :fdiv, :modulo, :abs, :magnitude, :zero?, :finite?, :infinite?, :floor, :ceil, :round, :truncate, :positive?, :negative?, :numerator, :denominator, :rationalize, :arg,Omitted below...
p "abc".methods
#[:encode, :encode!, :unpack, :unpack1, :include?, :%, :*, :+, :count, :partition, :to_c, :sum, :next, :casecmp, :casecmp?, :insert, :bytesize, :match, :match?, :succ!, :<=>, :next!, :upto,Omitted below...
p 8.0 / 3
p 8/3.0
p 8/3
#2.6666666666666665
#2.6666666666666665
#2
p 8.0/7
p (8.0/7).round
p (8.0/7).ceil
p (8.0/7).floor
#1.1428571428571428
#1 round
#2 ceil
#1 floor
calc = 8/3.0
puts format("%.2f", calc)
puts sprintf("%.2f", calc)
#2.67
#2.67
Object.round (number of digits)
Object.ceil (number of digits)
Object .floor (number of digits)
puts calc.round(2)
puts calc.round(3)
#2.67
#2.667
puts calc.ceil(2)
puts calc.ceil(3)
#2.67
#2.667
puts calc.floor(2)
puts calc.floor(3)
#2.66
#2.666
puts" # {formulas and variables} "
┗ Double quotation
┗ Output as it is if it is single
puts "tax #{1000 * 0.10}"
#tax 100.0
puts 'tax #{1000 * 0.10}'
#tax #{1000 * 0.10}
puts "Hello" + "World"
#HelloWorld
puts "Hello★" * 5
#Hello★Hello★Hello★Hello★Hello★
str = "hello world"
p str.length
#11
str = "123"
p str
p str.to_i
#"123"
#123
colors = ["red", "blue", "white"]
p colors[0]
p colors[3] #nil
p colors
#"red"
#nil
#["red", "blue", "white"]
colors = ["red", "blue", "white"]
p colors.push("green")
#["red", "blue", "white", "green"]
p colors << ("black")
#["red", "blue", "white", "green", "black"]
colors = ["red", "blue", "white", "green", "black"]
p colors.length
p colors.size
p colors.count
#5
#5
#5
colors = ["red", "blue", "white", "green", "black"]
p colors.sort
p colors.sort.reverse
#["black", "blue", "green", "red", "white"]
#["white", "red", "green", "blue", "black"]
numbers = [5, 8, 1, 4, 9]
p numbers.sort
p numbers.sort.reverse
#[1, 4, 5, 8, 9]
#[9, 8, 5, 4, 1]
** Create **
Variable = {key: value, key: value ,,,}
call
Variable [: key name]
scores = {tanaka:100, sato:80, ito:50}
p scores
p scores[:sato]
#{:tanaka=>100, :sato=>80, :ito=>50}
#80
All the same below
score1 = {"tanaka" => 100, "sato"=>80, "ito"=>50 }
score2 = {:tanaka =>100, :sato=>80, :ito=>50}
score3 = {tanaka:100, sato:80, ito:50}
p score1
p score2
p score3
#{:tanaka=>100, :sato=>80, :ito=>50}
#{:tanaka=>100, :sato=>80, :ito=>50}
#{:tanaka=>100, :sato=>80, :ito=>50}
scores = {tanaka:100, sato:80, ito:50}
scores[:suzuki] = 40
p scores
#{:tanaka=>100, :sato=>80, :ito=>50, :suzuki=>40}
scores = {tanaka:100, sato:80, ito:50, suzuki:40}
scores.delete(:suzuki)
p scores
#{:tanaka=>100, :sato=>80, :ito=>50}
if
Branch the process with ** conditional expression **.
if, elsif, else, end
python
stock = 5 #stock
if i >= 10
puts 'Stock: ◎'
elsif i > 3
puts 'Inventory: △'
elsif i == 1
puts 'Stock: Last 1'
else
puts 'Stock: ×'
end
#Inventory: △
Case syntax structure
#case Variable you want to evaluate
#when number
#processing
#when range (starting value)..closing price)
#processing
#when range (starting value)..closing price)
#processing
# else
#processing
# end
python
age = 6
case age
when 0..5
puts 800
when 6..11
puts 1800
else
puts 2500
end
#1800
・ Start price .. Close price
┗ Range from start price to close price
Array
x = ["A", 1, "b", 3]
for i in x
puts i
end
#A
#1
#b
#3
Consecutive numbers
for i in 0..3
puts i
end
#0
#1
#2
#3
python
arrays = ["A", 1, "b", 3]
arrays.each do |array|
puts array
end
#A
#1
#b
#3
python
i = 0
while i < 3
puts i
i += 1
# i++Cannot be used
end
#0
#1
#2
python
3.times do |i|
puts "#{i}This is the second process"
end
#This is the 0th process
#This is the first process
#This is the second process
i = 0
loop{
puts i
i += 1
if i >=3
break
end
}
#0
#1
#2
#3
python
3.times do |i|
if i == 1
next
end
puts "#{i}This is the second process"
end
#This is the 0th process
#This is the second process
▼ Creation
def method name () processing end
┗ () is not required when there is no argument.
▼ Call
Method name ()
python
def hello(name)
puts "Hello #{name}Mr."
end
hello("Yuta")
#Hello Yuta
Multiple arguments
def ttl_price(price, n)
return (price * n * 1.10).floor #return is optional
end
puts ttl_price(500, 3)
#1650
class class name end
--Class names are capitalized. --Initialize method: A method that is automatically called when an instance is created. --Properties are defined by @.
Creating a class
class User
def initialize(name)
@name = name
end
def say_name
puts "I#{@name}is."
end
end
Variable = class name.new (argument)
Instance generation
tanaka = User.new("tanaka")
tanaka.say_name
#I'm tanaka
yamada = User.new("yamada")
yamada.say_name
#I'm yamada
Recommended Posts