Ruby learning 4

Methods and so on 4

Currently, I am studying to get the Ruby engineer certification exam silver. I still have a lot of understanding of the language, so I will output from the basics.

Object ID

Excerpt from a mock question

foo = [1,2,3]
bar = foo
baz = foo.dup #Copy object

bar[3] = 4
p foo
p bar
p baz

=>[1, 2, 3, 4]
  [1, 2, 3, 4]
  [1, 2, 3]

What is the principle that the update of bar is applied to the variable foo?

foo = [1,2,3]
bar = foo
baz = foo.dup

p foo.object_id #Output the referenced object id
p bar.object_id
p baz.object_id

=>70275736077180
  70275736077180
  70275736077160

Since foo and bar refer to the same object, when bar is updated, foo is entangled and updated. On the other hand, baz is a duplicate of an object, so the reference target is different.

split method

Generates an array using the character string specified in the first argument as the delimiter.

servant = "saber,archer,lancer,rider,assassin,caster,berserker"
p servant.split!(/,/)

=> ["saber", "archer", "lancer", "rider", "assassin", "caster", "berserker"]

By specifying a numerical value in the second argument, the number of elements in the array can be specified.

servant = "saber,archer,lancer,rider,assassin,caster,berserker"
p servant.split!(/,/, 3)

=> ["saber", "archer", "lancer,rider,assassin,caster,berserker"]
#The third element is not separated and is grouped together.

delete method (String)

Deletes the character specified by the argument from self. Add a! To make it a destructive method.

puts "0123456789".delete("0-58") #"-"You can specify the range by sandwiching(In this case a number from 0 to 5)

=> "679"

To_Be_Continued...

Recommended Posts

Ruby learning 4
Ruby learning 5
Ruby learning 3
Ruby learning 2
Ruby learning 6
Ruby learning 1
Ruby Learning # 25 Comments
Ruby Learning # 13 Arrays
Ruby Learning # 1 Introduction
Ruby Learning # 34 Modules
Ruby Learning # 14 Hashes
Ruby Learning # 33 Inheritance
Ruby Learning # 15 Methods
Ruby Learning # 30 Initialize Method
Ruby learning points (basic)
Ruby Learning # 29 Classes & Objects
Ruby Learning # 20 Case Expressions
Ruby Learning # 24 Exponent Method
Ruby Learning # 17 If Statements
Ruby Learning # 21 While Loops
Ruby Learning # 31 Object Methods
Ruby Learning # 27 Writing Files
Ruby Learning # 35 Interactive Ruby (irb)
Ruby Learning # 9 Math & Numbers
Ruby Learning # 28 Handling Errors
Ruby Learning # 26 Reading Files
Ruby Learning # 23 For Loops
Ruby Learning # 16 Return Statement
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby Learning # 18 If Statements (Con't)
Ruby on Rails basic learning ①
Ruby Learning # 11 Building a Calculator
Ruby Learning # 99 Personal Programming Notebook
Ruby on rails learning record-2020.10.07 ②
Ruby Learning # 10 Getting User Input
Ruby Learning # 32 Building a Quiz
Ruby Learning # 8 Working With String
Ruby on rails learning record-2020.10.07 ①
Ruby on rails learning record -2020.10.06
Ruby Learning # 19 Building a Better Calculator
[Ruby] Array
Completed Progate Ruby Learning Course III
Ruby basics
Ruby basics
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Ruby Review 2
Ruby addition
Refactoring Ruby
Completed Progate Ruby Learning Course II
Servlet learning
Ruby Learning # 22 Building a Guessing Game
Learning output ~ 11/3 ~
Ruby setting 2
Ruby problem ⑦
Maven learning
[Ruby] Block
Refactoring Ruby
ruby calculator