[RAILS] Review the basic knowledge of ruby that is often forgotten


It will be an article to review the basic notation of ruby again.

-To_s method that converts an object to a string

#Character string → character string
'1'.to_s   # => "1"

#Numerical value → character string
1.to_s     # => "1"

#nil → blank string
nil.to_s   # => ""

#Boolean value true → of character string"true"
true.to_s   # => "true"

#Boolean false → string"false"
false.to_s   # => "false"

How to call a method

#Normal writing
object.Method(Argument 1,Argument 2,Argument 3)
#You may omit the parentheses
object.Method argument 1,Argument 2,Argument 3
#With no arguments

How to make a variable name

#Variable name should be snake case
first_name = user.first_name

#Camel case is not customarily used
firsfName = user.first_name

#Variables starting with a number will result in an error and cannot be used
2_discount_price = 200

About character strings

A string can be represented using either single quotes ('') or double quotes (""). Basically, use single quotes, and use double quotes when expanding expressions.

'This is a string'
"This is a string"

#When expanding an expression
i = 'String'
"this is#{i}"  # => this is文字列

Logical operator||When&&

# &&Is an AND logical operation
#True if both condition 1 and condition 2 are true, false otherwise
Condition 1&&Condition 2

t1 = true
t2 = true
f1 = false
t1 && t2      # => true
t1 && f1      # => false

||Is an OR logical operation

##Condition 1 is true if either condition 2 is true, and false if both are false.
Condition 1||Condition 2

t1 = true
t2 = true
f1 = false
f2 = false
t1 || t2      # => true
t1 || f1      # => true
f1 || f2      # => false

Methods ending in?

Methods ending in? Are customarily methods that return boolean values.

#Returns true if it is an empty string, false if it contains characters empty?Method
''.empty?           # => true
'AIUEO'.empty?   # => false

Recommended Posts

Review the basic knowledge of ruby that is often forgotten
Review of "strange Java" and Java knowledge that is often forgotten in Java Bronze
Review of Ruby basic grammar
Basic knowledge of Ruby on Rails
Determine that the value is a multiple of 〇 in Ruby
[Ruby] Basic knowledge of class instance variables, etc.
A description of Interface that is often mistaken
Basic methods of Ruby hashes
Basic knowledge of SQL statements
Basic methods of Ruby arrays
What is the representation of domain knowledge in the [DDD] model?
(Ruby on Rails6) Display of the database that got the id of the database
I tried to summarize the basic grammar of Ruby briefly
[Ruby] List of basic commands
The story of making a binding for libui, a GUI library for Ruby that is easy to install
Want to know what Ruby n is the power of 2? (Power judgment of 2)
[Swift] The story that switch is often used for enum judgment
About the behavior of ruby Hash # ==
Understand the basic mechanism of log4j2.xml
'% 02d' What is the percentage of% 2?
Extraction of "ruby" double hash * Review
[Ruby] See the essence of ArgumentError
The basic basis of Swift dialogs
The basic basis of Swift's Delegate
This and that of the JDK
Basics of Ruby ~ Review of confusing parts ~
[Ruby] Review about nesting of each
[Basic knowledge of Java] Scope of variables
Ruby Basics 2 ~ Review of confusing parts ~
[Ruby] Display the contents of variables
To you who lament that the conversion of JODConverter + LibreOffice is slow
[Introduction to Java] I tried to summarize the knowledge that I think is essential
With ruby ● × Game and Othello (basic review)
What is testing? ・ About the importance of testing
part of the syntax of ruby ​​on rails
Basic knowledge of Java development Note writing
[Ruby] Cut off the contents of twitter-ads
Ruby from the perspective of other languages
What is the data structure of ActionText?
[Basic knowledge of Java] About type conversion
The basic basis of Swift custom cells
Java engineers now compare to learn the basic grammar of Ruby Part 1 (Basic, Variables)
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
Initialize Ruby array with 0 like Java, that is, set the default value to 0
Upcast (Java) that can reduce the amount of change when the specification is changed
[Java] Where is the implementation class of annotation that exists in Bean Validation?
Implementation of vertical and horizontal scrolling that is often seen on Android recently
[Ruby on Rails] Implement a pie chart that specifies the percentage of colors