Getting Started with Ruby

irb 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

variable

abc = "name" → String

Naming method

Snake case

sample_name

Camel case

sampleMessage

comment

"#Comment out"

Method

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

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
 end

 def samplemethod2
  @number
 end
end

#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
 end

 def samplemethod2
  number
 end
end

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

Getter setter

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

operator

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

Authenticity

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'
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

Postfix if

puts "this is output" if true puts "this is not output" if false

Array

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

hash

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

Getting Started with Ruby
Getting Started with Ruby Modules
Getting Started with DBUnit
Getting Started with Swift
Getting Started with Docker
Getting Started with Doma-Transactions
Getting Started with Ruby for Java Engineers
Getting Started with Doma-Annotation Processing
Getting Started with JSP & Servlet
Getting Started with Java Basics
Getting Started with Spring Boot
Getting Started with Java_Chapter 5_Practice Exercises 5_4
[Google Cloud] Getting Started with Docker
Getting started with Java lambda expressions
Getting Started with Docker with VS Code
I started Ruby
Getting Started with Doma-Criteria API Cheat Sheet
Getting Started with Docker for Mac (Installation)
Getting Started with Parameterization Testing in JUnit
Getting Started with Java Starting from 0 Part 1
Getting Started with Ratpack (4)-Routing & Static Content
Getting started with the JVM's GC mechanism
Getting Started with Language Server Protocol with LSP4J
Getting Started with Creating Resource Bundles with ListResoueceBundle
Getting Started with Java_Chapter 8_About Instances and Classes
Install Ruby 3.0.0 with asdf
Links & memos for getting started with Java (for myself)
Getting Started with Doma-Using Subqueries with the Criteria API
Getting Started with Java 1 Putting together similar things
Getting started with Kotlin to send to Java developers
Getting Started with Doma-Using Joins with the Criteira API
Getting Started with Doma-Introduction to the Criteria API
Get started with Gradle
11th, Classification with Ruby
Evolve Eevee with Ruby
I tried Getting Started with Gradle on Heroku
Getting started with Java programs using Visual Studio Code
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Ruby version switching with rbenv
Solve Google problems with Ruby
I tried DI with Ruby
GraphQL Client starting with Ruby
Get started with Spring boot
Leap year judgment with Ruby
Format Ruby with VS Code
Integer check method with ruby
Get started with DynamoDB with docker
Ruby Learning # 10 Getting User Input
Ruby Learning # 8 Working With String
Completable Future Getting Started (First Future)
[Ruby] problem with if statement
Studying with CodeWar (ruby) ⑤ proc
Use Ruby with Google Colab
[Ruby] REPL-driven development with pry
[ruby] Method call with argument
Proceed with Rust official documentation on Docker container (1. Getting started)
Getting started with Java and creating an AsciiDoc editor with JavaFX
Getting Started with Doma-Dynamicly construct WHERE clauses with the Criteria API
Getting Started with Reactive Streams and the JDK 9 Flow API
Getting Started with GitHub Container Registry instead of Docker Hub
Let's get started with parallel programming