irb Interactive execution environment
"String" (receiver) .class → String 1.class → Integer
"Character string" .object_id → Another object is created each time it is executed
message1 = "message 1" message2 = "message 2" message1.concat (message2) * Parentheses can be omitted message1 = message 1 message 2
Data such as words and sentences composed of letters and symbols that can be read by humans Enclose the content in double quotes (single quotes are also possible)
Object representing a number
What kind of function an object can have depends on what class of object the object appears in. In addition to the classes provided by Ruby as standard (embedded library / standard attached library), you can also create your own classes.
abc = "name" → String
sample_name
sampleMessage
"#Comment out"
The behavior of Ruby objects is a method. "A dog (class) wheat (instance) has the ability to lie (method) to humans." = Wheat. Tell a lie (human)
You can define chasing a dog class and call the "lie" method on any dog object, not just wheat.
class dog
def tell a lie (human)
puts "Dog#{Human}Lie to..."
end
end
Wheat=dog.new
Variables held by the object. It can be used from within any method of the object. Be sure to prefix the name with @.
class Sample
def samplemethod1
@number =100 # instance variable
end
def samplemethod2
@number
end
end
#object = Sample.new
#object.samplemethod1 is also object.samplemethod2 can also be called
An ad hoc temporary variable. Local variables defined within a method can only be used within that method.
class Sample
def samplemethod1
number =100 # local variables
end
def samplemethod2
number
end
end
#object = Sample.new
#object.samplemethod1 can be called, but object.samplemethod2 cannot be called
class User
def name=(name) #Setter
@name = name
end
def name #Getter
@name
end
end
#↓ Easy way to write ↓
class User
attr_accessor :name, :address, :email
end
Left align | Left align |
---|---|
+ | Add, concatenate strings, concatenate arrays |
- | Draw, remove some elements from the array |
* | Multiply, repeatedly concatenate character strings, repeatedly concatenate arrays |
/ | Divide |
% | Get the remainder |
&& / and | AND operation |
^ | XOR operation |
! / not | Turn over the truth (denial) |
= | Substitution |
== | Find out if they are equal |
!= | Find out if they are not equal |
>, >=, <, <= | The left side is large, the left side is more than the right side, the right side is larger, the right side is more than the left side |
nil Empty state sample = nil? Find out if it is nil
In Ruby, nil and false are false, and the others are true (0 is also true).
number = 1
if number == 1
puts 'The number is 1'
elseif number == 2
puts 'The number is 2'
else
puts 'Numerical values other than 1 and 2'
end
#Expression using unless
age = 20
unless age >= 20
puts "I don't have the right to vote "
end
#Expression using if
age = 20
if age < 20
puts "I don't have the right to vote "
end
puts "this is output" if true puts "this is not output" if false
Structure in which multiple elements are stored in order array = ["123", false, nil, 1, [a,b,c]] Add element array << a
array = [1,2,3]
array.each do |element|
puts element
end
class User
attr_accessor
end
user1 = User.new
user1.name = 'mayu'
user2 = User.new
user2.name = 'ayako'
user3 = User.new
user3.name = 'kenji'
users = [user1, user2, user3]
#If you want to get one by one,
#Method ① Use each
names = []
users.each do |user|
names << user.name
end
p names
#==>["mayu", "ayako", "kenji"]
#Method ② Use map
names = users.map do |user|
user.name
end
#==>["mayu", "ayako", "kenji"]
#Omission of method ② ①
names = users.map{ |user| user.name }
#==>["mayu", "ayako", "kenji"]
#Omission of method ②
names = users.map(&:name)
#==>["mayu", "ayako", "kenji"]
Data structure that internally stores data in association with keys
Various notations
#Use a character string as a key
{"student1" => mayu, "student2" => asami}
#Use strings as keys and use colons instead of hash rockets
{"student1": mayu, "student2": asami}
#Use the symbol as a key
{:student1 => mayu, student2 => asami}
#Symbol as a key,Use a colon instead of a hash rocket * General
{student1: mayu, student2: asami}
Get value
array = {:student1 => mayu, student2 => asami}
puts array[:student1]
#Output as mayu
Update value
array = {:student1 => mayu, student2 => asami}
array[:student1] = 'misaki'
puts array[:student1]
#Output as misaki
[Reference | Ruby on Rails 5 Quick Learning Practice Guide that can be used in the field](https://www.amazon.co.jp/%E7%8F%BE%E5%A0%B4%E3%81%A7%E4%BD % BF% E3% 81% 88% E3% 82% 8B-Ruby-Rails-5% E9% 80% 9F% E7% BF% 92% E5% AE% 9F% E8% B7% B5% E3% 82% AC % E3% 82% A4% E3% 83% 89-% E5% A4% A7% E5% A0% B4% E5% AF% A7% E5% AD% 90 / dp / 4839962227)
Recommended Posts