Ruby Strings

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Ruby Strings

Ruby string object holds and manipulates an arbitary sequence of bytes, typically representing characters. They are created using String::new or as literals.


Quotes

Ruby string literals are enclosed within single and double quotes.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. puts 'Hello everyone'   
  4. puts "Hello everyone" 

Output:

Ruby string 1

Accessing string elements

You can access Ruby string elements in different parts with the help of square brackets []. Within square brackets write the index or string.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. msg = "This tutorial is from JavaTpoint."   
  4.   
  5. puts msg["JavaTpoint"]   
  6. puts msg["tutorial"]   
  7.   
  8. puts msg[0]   
  9.   
  10. puts msg[0, 2]   
  11. puts msg[0..19]   
  12. puts msg[0, msg.length]   
  13. puts msg[-3]

Output:

Ruby string 2

Multiline string

Writing multiline string is very simple in Ruby language. We will show three ways to print multi line string.

String can be written within double quotes. The % character is used and string is enclosed within / character.

Example:

  1. puts "   
  2. A   
  3. AB   
  4. ABC   
  5. ABCD"   
  6.   
  7. puts %/   
  8. A   
  9. AB   
  10. ABC   
  11. ABCD/   
  12.   
  13. puts <
  14. A   
  15. AB   
  16. ABC   
  17. ABCD   
  18. STRING   

Output:

Ruby string 3

Variable Interpolation

Ruby variable interpolation is replacing variables with values inside string literals. The variable name is put between #{ and } characters inside string literal.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. country = "India"   
  4. capital = "New Delhi"   
  5.   
  6. puts "#{capital} is the capital of #{country}."  

Output:

Ruby string 4

Concatenating Strings

Ruby concatenating string implies creating one string from multiple strings. You can join more than one string to form a single string by concatenating them.

There are four ways to concatenate Ruby strings into single string:

  • Using plus sign in between strings.
  • Using a single space in between strings.
  • Using << sign in between strings.
  • Using concat method in between strings.

Example:

Output:

Ruby string 5

Freezing Strings

In most programming languages strings are immutable. It means that an existing string can't be modified, only a new string can be created out of them.

In Ruby, by default strings are not immutable. To make them immutable, freeze method can be used.

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. str = "Original string"   
  4. str << " is modified "   
  5. str << "is again modified"   
  6.   
  7. puts str   
  8.   
  9. str.freeze   
  10.   
  11. #str << "And here modification will be failed after using freeze method"

Output:

Ruby string 6

In the above output, we have made the string immutable by using freeze method. Last line is commented as no string can't be modified any further.

By uncommenting the last line, we'll get an error as shown in the below output.

Output:

Ruby string 7

Comparing Strings

Ruby strings can be compared with three operators:

  • With == operator : Returns true or false
  • With eql? Operator : Returns true or false
  • With casecmp method : Returns 0 if matched or 1 if not matched

Example:

  1. #!/usr/bin/ruby   
  2.   
  3. puts "abc" == "abc"   
  4. puts "as ab" == "ab ab"   
  5. puts "23" == "32"   
  6.   
  7. puts "ttt".eql? "ttt"   
  8. puts "12".eql? "12"   
  9.   
  10. puts "Java".casecmp "Java"   
  11. puts "Java".casecmp "java"   
  12. puts "Java".casecmp "ja"  

Output:

Ruby string 8