[Ruby basics] split method and to_s method

The split method is a method that "separates character strings one by one and makes them into an array".

def split_1st(string) #Method that expects string as an argument
  string.split("") #to_Note that there is no s method.
end
p split_1st("12345")
#=> ["1","2","3","4","5"]
p split_1st("Thank You")
#=> ["T","h","a","n","k"," ","Y","o","u"]

If a space is included like "Thank You", the space is also regarded as one of the character strings and output.

What if the argument is a number rather than a string? If you add the to_s method as shown below, you can also separate the numbers into an array.


def split_2nd(integer) #A method that expects an integer as an argument
  integer.to_s.split("") #to_Convert the numerical value to a character string with the s method, and separate the character strings one by one with the split method.
end
p split_2nd(12345)
#=> ["1","2","3","4","5"]
p split_2nd("12345") #This call to the original string is also validly printed.
#=> ["1","2","3","4","5"]

Recommended Posts

[Ruby basics] split method and to_s method
Ruby to_s method
Ruby basics
Ruby basics
Ruby basics
[Ruby] From the basics to the inject method
ruby Exercise Memo II (variable and method)
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
Basics of Ruby
variable and method
[Ruby] initialize method
Ruby build method
Ruby and Gem
Ruby accessor method
[Java] split method
ruby map method
[Ruby] How to use gsub method and sub method
Ruby on Rails ~ Basics of MVC and Router ~
[Ruby basics] How to use the slice method
[Ruby] Classes and instances
Ruby Learning # 30 Initialize Method
Symbols and Destructive Ruby
[Ruby] Big Decimal and DECIMAL
[Ruby] Exception handling basics
Ruby Thread # [] = method notes
Ruby on Rails basics
definition of ruby method
Ruby classes and instances
Ruby inheritance and delegation
Ruby variables and methods
[Ruby] Class nesting, inheritance, and the basics of self
Be careful about Ruby method calls and variable references
[Ruby] Method definition summary
About the to_s method.
[Ruby] Questions and verification about the number of method arguments
Docker Compose basics and commands
GraphQL Ruby and actual development
[Rails] require method and permit method
Ruby syntax errors and countermeasures
Integer check method with ruby
Ruby algorithm (inject, method definition)
About Ruby hashes and symbols
Ruby C extension and volatile
Summarize Ruby and Dependency Injection
startsWith method / endsWith method, split method, append method
Java methods and method overloads
[Ruby] Notes on gets method
About Ruby and object model
[Ruby] Method that returns truth
[Ruby] Singular methods and singular classes
About Ruby classes and instances
Ruby variables and functions (methods)
Ruby methods and classes (basic)
Creating Ruby classes and instances
[ruby] Method call with argument
[Ruby] Singular methods and singular classes
Easy to understand the difference between Ruby instance method and class method.