[Ruby] Getting Started with Ruby

4 minute read

Interactive execution environment

Check the object type

“String” (receiver) .class → String
 1.class → Integer

Check object_id

“Character string” .object_id → Another object is created each time it is executed

  1. object_id → The same numeric object is provided
    Combine objects

    message1 = “message 1”
    message2 = “message 2”
    message1.concat (message2) * Parentheses can be omitted
    message1 = message 1 message 2

String object

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)

Numeric object

Object representing a number

Classes and instances

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.

Screen Shot 2020-07-25 at 22.42.22.png

Screen Shot 2020-07-26 at 0.15.32.png


abc = “name”
→ String

Naming method

Snake case


Camel case


  • Names starting with a capital letter are interpreted as “constants” whose values are invariant.


“#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)

  • Lie = instance method

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..."


Instance variables

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

 def samplemethod2

#object = Sample.new
#object.samplemethod1 is also object.samplemethod2 can also be called

Local variables

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

 def samplemethod2

#object = Sample.new
#object.samplemethod1 can be called, but object.samplemethod2 cannot be called

Getter setter

class User
 def name=(name) #Setter
  @name = name

 def name #Getter

#↓ Easy way to write ↓

class User
 attr_accessor :name, :address, :email


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    
    / or OR operation
&& / 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    

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).

Conditional branch

number = 1
if number == 1
 puts 'The number is 1'
elseif number == 2
 puts 'The number is 2'
 puts 'Numerical values other than 1 and 2'
#Expression using unless
age = 20
unless age >= 20
 puts "I don't have the right to vote "
#Expression using if
age = 20
if age < 20
 puts "I don't have the right to vote "

Postfix if

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
class User

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
p names
#==>["mayu", "ayako", "kenji"]

#Method ② Use map
names = users.map do |user|
#==>["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)