What kind of writing will give the expected value when the pattern is to arrange the character strings and make the array into a character string?
So, let's organize it with some bad patterns!
We will use the split method
.
** split English translation: split ** In other words, it is a method that divides a cohesive object (character string, etc.) into elements. (Arrangement) The argument is * an image that specifies the boundary of the part to be cut *
I tried to arrange from various cohesive patterns.
The array you want to have is [" foo "," bar "," baz "]
.
Now, which writing style is right for you?
#Arrangement#Return value
"foo bar baz".split ["foo", "bar", "baz"] # ○
"foo bar baz".split('') ["f", "o", "o", " ", "b", "a", "r", " ", "b", "a", "z"]
"foo bar baz".split(',') ["foo bar baz"]
"foobarbaz".split ["foobarbaz"]
"foobarbaz".split('') ["f", "o", "o", "b", "a", "r", "b", "a", "z"]
"foobarbaz".split(',') ["foobarbaz"]
"fooxbarxbaz".split('x') ["foo", "bar", "baz"] # ○
"foo, bar, baz".split ["foo,", "bar,", "baz"]
"foo, bar, baz".split('') ["f", "o", "o", ",", " ", "b", "a", "r", ",", " ", "b", "a", "z"]
"foo, bar, baz".split(',') ["foo", " bar", " baz"]
"foo,bar,baz".split ["foo,bar,baz"]
"foo,bar,baz".split('') ["f", "o", "o", ",", "b", "a", "r", ",", "b", "a", "z"]
"foo,bar,baz".split(',') ["foo", "bar", "baz"] # ○
%w[foo bar baz] ["foo", "bar", "baz"] # ○
From this, there are * 4 ways * of how to write the expected array.
We will use the join method
.
It will be an image of inserting the value to be inserted between * elements in the argument *.
#Stringification#Return value
["foo", "bar", "baz"].join "foobarbaz"
["foo", "bar", "baz"].join('') "foobarbaz"
["foo", "bar", "baz"].join(',') "foo, bar, baz"
#Arrange the range#Return value
(0..9).to_a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
('a'..'z').to_a ["a",..,"z"]
(1..5).map{ |i| i**2 ) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Recommended Posts