I was confused because there was a split in the Array

When I wrote the following code, the array of arrays came back and I was confused.

numbers.split(/,/) #=> [["1","2","3"]]

The cause was that I was calling split for a split array.

numbers = "1,2,3".split(/,/)
numbers.split(/,/) #=> [["1","2","3"]]

Rails Active Support provides Array # split. It splits an array into an array of arrays before and after a particular value.

[1, 2, 3, 4, 5].split(3) # => [[1,2],[4,5]]

However, if the variable was supposed to contain a string but was an array, it would be annoying.

def include_three?(string)
  string.split(/,/).include?("3")
end

include_three?("1,2,3,4,5".split(/,/)) #=> false

Recommended Posts

I was confused because there was a split in the Array
[When using MiniMagick] A memorandum because I stumbled in the CircleCI test environment.
I was addicted to the NoSuchMethodError in Cloud Endpoints
Calculate the difference between numbers in a Ruby array
I got stuck in a clone of a two-dimensional array
A memorandum because I was addicted to the setting of the Android project of IntelliJ IDEA
Use a binary search to see if there are any values in the array
Review because I used the collection_check_boxes method in the Ralis portfolio
[Java] ArrayList → Should I specify the size in array conversion?
Multiplication in a Ruby array
A story I was addicted to in Rails validation settings
How to add the same Indexes in a nested array
I have a question because the gradle setup doesn't work
In Ruby code, I was confused for a moment as "Hash-like but parentheses are arrays?"
What if I write a finally clause in the try-with-resources syntax?
My site was thrown in a week after joining the company
I recently made a js app in the rumored Dart language
How to output the value when there is an array in the array
I checked because the response was strange when debugging with Tomcat 8
Sorting hashes in a Ruby array
I created a PDF in Java.
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
A story I was addicted to when testing the API using MockMVC
I was in trouble at work, so I made a plugin for IntelliJ
The parameters I received in Rails were a bit different than I expected
Recorded because I was addicted to the standard input of the Scanner class
I was addicted to scrollview because I couldn't tap the variable size UIView
I thought about the best way to create a ValueObject in Ruby
I was addicted to unit testing with the buffer operator in RxJava
I want to ForEach an array with a Lambda expression in Java
Since the unit test of the PJ I was in charge of was a hell picture, I will publish my own guide (?)
When I wanted to create a method for Premium Friday, it was already in the Java 8 standard API
[Ruby] I want to make an array from a character string with the split method. And vice versa.
I tried a calendar problem in Ruby
I tried the new era in Java
Organized memo in the head (Java --Array)
Creating a Servlet in the Liberty environment
I was addicted to the Spring-Batch test
Split a string with ". (Dot)" in Java
I tried the AutoValue library in Intellij
I tried embedding a formula in Javadoc
Character string comparison: I was caught in the skill check problem of Paiza
(Capistrano) After deploying, I get a We're sorry… error in the production environment.
[Java] I checked it because it was erratic when converting the type. <Cast operator>
I was a little addicted to the S3 Checksum comparison, so I made a note.
Isn't there a name collision when enum has the same name in the switch statement?
SpringSecurity I was addicted to trying to log in with a hashed password (solved)
When I think about the 402 error that suddenly appeared in the middle of the introduction of PAY.jp, there was an unexpected place
Even if I want to convert the contents of a data object to JSON in Java, there is a circular reference ...