I already make a post about strings, This will have all what I put on the prior post and MORE. so here we go.

what is a string?  well the way I see it is like a word, a word or a phrase: is a bunch of letter(charactes) with or without spaces, in a single chunk. in ruby is the same just that you need to begin and end with: ”

How we can initialize strings.

Escape characters

the same way there are other escape sequences that can be used:

  • \" – double quote
  • \\ – single backslash
  • \a – bell/alert
  • \b – backspace
  • \r – carriage return
  • \n – newline
  • \s – space
  • \t – tab

now lest manipulate this strings a little.

Center, and justify strings.

String Interpolation

another thing is String Interpolation which I always do with: “String #{variable} more string”   but there is another way using the: %s which  expects a string and the: %d which expects a number. see example

Concatenating Strings

Freezing Stings.

strings you can modify them, but once you freeze that string you will have an error when trying modify it.

Comparing strings

Spaceship Operator <==>

basically will compare two strings return: (1) when (left > right),  (-1) when (left < right), (0) when (left == right).


in case that you have words you can use: “casecmp” operator that will do the same but with strings.

Strings Methods

Here is where the fun begings because the methods are like the superpowers that the Objects (in this case can use) so lest see some of the superpowers that an object “string” can use:

On this post we will show how to use some of the methods but there are too many, and some are rarely used so here is a link to a website that have a list of them  LINK

Now some examples:

“.empty?”  “.clear”

In this example we have a string with the word “Hello Edmundo” ten we use the method “.empty?” and “.clear”

“.upcase”  –  “.downcase”  –  “.capitalize” –  “.swapcase”

“.start_with?”  –  “.end_with?”

Next super powers are “.start_with?”  &  “.end_with?” this will return booleans and this is how we use this powers(methods).


The next one is like x-Rays to a string, to able to see the Escape sequences on the string.

Print-Format using  “%”

This super power it allow us to be able to pass and format some information when printing see example

For a more complete list of Print-format:


There are literally lots of Super-Powers (a.k.a Methods) for a more complete list see the link given before. one that is very useful is the ability to create an Array from the data on the sting and for this we will use the super power: “.split”

the next example uses the same “.split” method with the difference that instead of using the the “” to separate we will use an space ” ” and also a comma “,”

if you see the example above, you will see that when I use the space ” ” in the end Array I cut “New York” in two different parts, and in the other case I use the comma “,” it works just fine, so make sure that your strings are limited by the right characters.

Now a little more on Formatting,

In this case we have an Array of Arrays, with the information of the states and the population, and we use the method “.each” and we use an “Block” that take as parameters the data on each of the sub Arrays, and prints it in the variable “name” (that is created on the moment and is recreated on each recursion)  the data  on the index[0] of the sub Array, on the variable “name” we are using another method: “.ljust” this is the one that, will provide spaces or in other words “Justification”, then we use the “+” to concatenate the next data of the Array with index[1] and is saved on the variable “age” to this variable we apply the method: “.to_s” this method converts the number to a string.   well enough of explanations the following is the example executed on Ruby.




Copyright 2017. All rights reserved.

Posted January 4, 2017 by Edmundo in category "Ruby