Computer Science‎ > ‎

Introduction to Ruby - Strings



Programming With Ruby


As per the official definition "String object holds and manipulates an arbitrary sequence of bytes, typically representing characters". Let's try out a couple of different ways to initialize strings. And then let's manipulate those strings a bit; and in the process 

check out some of the interesting methods which Ruby offers.









# Trying out different ways to initialize a string


irb(main):063:0> subjectName = "Calculus"

=> "Calculus"

irb(main):064:0> subjectCode = "MA101"

=> "MA101"

irb(main):065:0> department = String.new("Mathematics")

=> "Mathematics"

irb(main):066:0> hoursPerWeek = 5.to_s

=> "5"


# Using a string formatting method, building it up from other strings 


irb(main):067:0> courseDescription = "#{subjectName} #{subjectCode} #{department}" 

=> "Calculus MA101 Mathematics 5"

# Splitting the string by specifying a delimiter which could either be a string or a regular expression

irb(main):079:0> tokens = completeCourseDescription.split(/\s/)
=> ["Calculus", "MA101", "Mathematics", "5"]

# Accessing a Character at a particular index in the string

irb(main):080:0> completeCourseDescription[0]
=> "C"

# sub is used to substitute one string by another - but only the first instance In this case we will replace the first occurrence of the space by a colon

irb(main):082:0> completeCourseDescription.sub(" ",":")
=> "Calculus:MA101 Mathematics 5"

#gsub is used to substitute one string by another - all instances. In this case we will replace all the occurrences of a space by a colon

irb(main):083:0> completeCourseDescription.gsub(" ",":")
=> "Calculus:MA101:Mathematics:5"

# Gsub can also be used by passing in a regular expression in the first parameter. \1 Indicates the matched group [A-Z]

irb(main):085:0>completeCourseDescription.gsub(/ ([A-Z])/,'::\1')
=> "Calculus::MA101::Mathematics 5"

# Repeat a string

irb(main):086:0>completeCourseDescription * 2
=> "Calculus MA101 Mathematics 5Calculus MA101 Mathematics 5"

# Changing the Case of a string to all lower case characters
irb(main):087:0> completeCourseDescription.downcase
=> "calculus ma101 mathematics 5"

#Changing the case of a string to all upper case characters
irb(main):088:0> completeCourseDescription.upcase
=> "CALCULUS MA101 MATHEMATICS 5"

#Capitalize converts the first letter of each token/word to upper case.

irb(main):089:0> "test capitalize on this string".capitalize
=> "Test capitalize on this string"



Check out some of our other Ruby Tutorials :

Introduction to Ruby

 Introduction to Ruby and some playing around with the Interactive Ruby Shell (irb) Introduction to Ruby - Conditional statements and Modifiers: If-then, Unless, Case Introduction to Ruby Comments - Single and Multi-Line comments Introduction to Ruby Loops - Using While, Until, For, Break, Next , Redo, Retry
 Introduction to Ruby - Arrays - Sorting, Filtering (Select), Transforming, Multi-Dimensional Arrays Introduction to Ruby - Strings Introduction to Ruby - Making a Script Executable Introduction to Ruby - Regular Expressions, Match, Scan
 Introduction to Ruby - Computing Factorials Recursively : An Example of Recursion Introduction to Ruby - Binomial Coefficients (nCr) : An Example of Recursion Introduction to Ruby - Computing a Power Set : An Example of Recursion Introduction to Ruby - Towers of Hanoi : An Example of Recursion
 Introduction to Ruby - Strings: Substitution, Encoding, Built-In Methods 



Basic Data Structures With Ruby







Programming With Ruby

 Introduction to Ruby and some playing around with the Interactive Ruby Shell (irb)

Introduction to Ruby - Conditional statements and Modifiers: If-then, Unless, Case

Introduction to Ruby Comments - Single and Multi-Line comments

Introduction to Ruby Loops - Using While, Until, For, Break, Next , Redo, Retry

Introduction to Ruby - Arrays - Sorting, Filtering (Select), Transforming, Multi-Dimensional Arrays 

Introduction to Ruby - Strings

Introduction to Ruby - Making a Script Executable

Introduction to Ruby - Regular Expressions, Match, Scan

Introduction to Ruby - Computing Factorials Recursively : An Example of Recursion

Introduction to Ruby - Binomial Coefficients (nCr) : An Example of Recursion

Introduction to Ruby - Computing a Power Set : An Example of Recursion

Introduction to Ruby - Towers of Hanoi : An Example of Recursion

 Introduction to Ruby - Strings: Substitution, Encoding, Built-In Methods

Basic Data Structures in Ruby - Insertion Sort

Basic Data Structures in Ruby - Selection Sort

Basic Data Structures in Ruby - Merge Sort

Basic Data Structures in Ruby - Quick Sort

Functional Programming with Ruby

Basic Data Structures in Ruby - Stack

Basic Data Structures in Ruby - The Queue

Basic Data Structures in Ruby - Linked List - ( A Simple, Singly Linked List)

Basic Data Structures in Ruby - Binary Search Tree