• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Using Ruby to Convert String to Array

Using Ruby to Convert String to Array

October 13, 2022 Leave a Comment

In Ruby, we can convert a string to an array by using the split() method.

some_string = "string"
some_string = some_string.split("")

print some_string

#Output:
["s", "t", "r", "i", "n", "g"]

If we want to convert a string with spaces to an array and have each element in the array be separated by spaces, we just have to change the parameter in our split() method to include a space.

Here is the code to do this.

some_string = "this is a string with spaces";
some_string = some_string.split(" ")

print some_string

#Output:
["this", "is", "a", "string", "with", "spaces"]

When working with different objects in Ruby, the ability to be able to convert objects into other objects easily can be useful.

One such case is if you want to convert a string into an array in Ruby.

To convert a string into an array using Ruby, the easiest way is with the split() method.

The split() method creates a new array with elements containing each character of the string.

Below is our simple example again showing you how to convert a string to an array in Ruby.

some_string = "string"
some_string = some_string.split("")

print some_string

#Output:
["s", "t", "r", "i", "n", "g"]

Hopefully this article has been useful for you to learn how to convert a string to array in Ruby.

Other Articles You'll Also Like:

  • 1.  Using Ruby to Print to Console
  • 2.  Using Ruby to Reverse String
  • 3.  Ruby Ceiling – Find the Ceiling of a Number with the ceil Method
  • 4.  Using Ruby to Convert Array to String
  • 5.  Ruby Infinite Loop
  • 6.  Using Ruby to Get the Length of Array
  • 7.  Using Ruby to Capitalize First Letter of String
  • 8.  Ruby include Method – Check if Item in Array
  • 9.  How to Print Without Newline in Ruby
  • 10.  Using Ruby to Get the Length of a String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy