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