Ruby — Strings Under a Microscope
By: Justin Langlinais
Tags: Ruby, Ruby on Rails, Object Oriented, Web Development, Software Development

Photo by Vivienne Nieuwenhuizen on Unsplash

I did a deep dive into Ruby this week, with a huge chunk of my work being on Ruby Koans’ test-driven road to Ruby enlightenment. These are basically notes and things I learned along the way…

Everything in Ruby is an object, and has a unique object_id . There are some exceptions, including symbols, like :potato for example. Two instances of that would have the same object_id .

Even objects that seem identical have different object_ids . Take these strings, for example:

//> one = "a string"

//> two = "a string"

//> one == two

=> true

//> one.object_id == two.object_id

=> false

Interesting note: integers have odd, sequential object_ids :

//> 1.object_id

=> 3

//> 2.object_id

=> 5

//> 3.object_id

=> 7

//> 100.object_id

=> 201

Core Object types:

Strings

Arrays

Hashes

Symbols

Booleans

Numbers (Integers and Floats)

nil

For now, let’s explore Strings…

Strings

Quotes:

In Ruby, there are some distinctions between double quotes and single quotes.

//> "I like playing guard" == 'I like playing guard'

=> true

One important use of double quotes is interpolation, which is plugging in methods or variables into a string.

Interpolating a variable:

//> passes = 6

//> string = "I think I got #{passes} bags of doritos today."

//> string

=> "I think I got 6 bags of doritos today."

Interpolating a method:

//> "The square root of 9 is #{Math.sqrt(9)"

=> "The square root of 9 is 3"

It gets interesting when you put single quotes inside of double quotes, and vice versa:

//> "Don't berimbolo me"

=> "Don't berimbolo me" //> 'He asked me to "stop passing"'

=> "He asked me to \"stop passing\""

Use flexible quotes to handle complicated strings. Using % and then any other character to wrap your string as a delimiter will work:

//> %{This handles "double", 'single', & @LL kinds of $stuff}

=> "This handles \"double\", 'single', & @LL kinds of $stuff" //> %~This handles "double", 'single', & @LL kinds of $stuff~

=> "This handles \"double\", 'single', & @LL kinds of $stuff"

Combining strings:

Combine strings using the plus sign:

//> "This " + "works!"

=> "This works!"

Assign separate strings to variables and add those variables together to leave the originals unmodified:

//> leg = "Leg "

//> drags = "drags!"

//> passes = leg + drags //> passes

=> "Leg drags!"

//> leg

=> "Leg "

//> drags

=> "drags!"

You can do the same with the += operator, but it will modify the original variable. The same goes for the shovel << operator:

//> leg = "Leg "

//> drags = "drags!"

//> passes = leg += drags //> passes

=> "Leg drags!"

//> leg

=> "Leg drags!"

//> drags

=> "drags!"

Accessing substrings and single characters from a string.

Single characters can be accessed by specifying their index position:

//> string = "This is fun!"

//> string[1]

=> "h"

//> string[-1]

=> "!"

With substrings, you can use a range ( string[1..24] ) or specify the index and use a comma to indicate how many characters you want to capture ( string[1,4] ):

//> string = "Potatoes!"

//> string[4..8]

=> "toes!"

//> string[4,5]

=> "toes!"

Splitting and joining strings:

Ruby’s .split will split a string into an array using a space as the default split point:

//> string = "Armbar Triangle Omoplata"

//> drills = string.split

//> drills

=> ["Armbar", "Triangle", "Omoplata"]

You can also split on any other specific character:

//> string = "Justin,Lucy,Otto"

//> coolfolks = string.split(/,/)

//> coolfolks

=> ["Justin", "Lucy", "Otto"]

Using .join to turn an array into a string, using the argument as what joins them. In this case, it’ll be a space:

//> array = ["Initiate", "ultimate", "passing", "combo"]

//> strategy = array.join(" ")

//> strategy

=> "Initiate ultimate passing combo"

Obviously, this would involve leg drags.

Hope this is helpful. Please comment or reach out with anything you’d like to add or any questions. Thank you,

Resources: