7/27/2015 - 5:10 AM



1.1 String Basics

String Interpolation

It is essential to be able to replace placeholders within a string with values they represent.

a = 1
b = 4
puts "The number #{a} is less than #{b}"

The number 1 is less than 4

Complete the functionality of the method below which, given a String as the argument, inserts the length of that String into another String:

def string_length_interpolater(incoming_string)
  "The string you just gave me has a length of #{incoming_string.length}"

Search in a String

Another common scenario is checking if a String contains any given character, word or sub-string.

I can’t believe it. [Yoda:] That is why you fail.".include? "Yoda"

detects when the string includes 'Yoda' ✔

"Ruby is a beautiful language".start_with? "Ruby"

detects whether the string starts with 'Ruby'

"I can't work with any other language but Ruby".end_with? "Ruby"

detects whether the string ends with 'Ruby'

It is conventional in Ruby to have '?' at the end of the method if that method returns only boolean values.

Find if this contain letter R

"I am a Rubyist".index('R')

String case change

Manipulate the case of strings converting a string in lower case to upper case.puts 'i am in lowercase'.upcase #=> 'I AM IN LOWERCASE'

puts 'i am in lowercase'.upcase #=> 'I AM IN LOWERCASE'

.downcase .swapcase

1.2 Advanced String Operations

Methods that the string object provide fro string manipulation

Splitting Strings

puts 'i am in lowercase'.upcase #=> 'I AM IN LOWERCASE'

Concatenating Strings

Two ways to do the same thing

'Ruby' + 'Monk'

The literal and expressive method


Replacing a substring

The Ruby String API provides strong support for searching and replacing within strings. We can search for sub-strings or use Regex.

Replace first ocurrence 'I' with 'We' in a given string:

"I should look into your problem when I get time".sub('I','We')

Replace both

"I should look into your problem when I get time".gsub('I','We')

Regular Expressions or RegExs are a concise and flexible means for "matching" particular characters, words, or patterns of characters.


Could you replace all the characters in capital case with number '0'

'RubyMonk Is Pretty Brilliant'.gsub(/[A-Z]/,'0')

Find a substring using RegEx

This is how you find the characters from a String which are next to a whitespace:

'RubyMonk Is Pretty Brilliant'.match(/ ./)

passes two arguments, a regular expression and a starting point to the 'match' method

'RubyMonk Is Pretty Brilliant'.match(/ ./, 9)

2.0 Boolean Expressions in Ruby

Methods that the string object provide fro string manipulation

Splitting Strings

puts 'i am in lowercase'.upcase #=> 'I AM IN LOWERCASE'

0.0 Objects

Introduction to Objects

In Ruby, just like in real life, our world is filled with objects. Everything is an object - integers, characters, text, arrays

Talking to objects

One object interacts with another by using what are called methods We call method even? on the object number 2 by placing a . after the object


Invoking a method on an object generate a response (2.even? true )

Call the method twice

0.1 More Objects and Methods

Looking up methods

Ruby objects are happy to tell you what methods they provide. You simply call the methods method on them.


As you can see, you get a listing of all the methods on the number 1 that you could invoke. And you can sort them

Invoking methods with arguments

When talking to an object via its methods, it is possible to give it additional information so it can give you an appropriate response.

This additional information is called the "arguments to a method." methods are the paths of communication between objects.

Here's an example of an argument to the method index, which finds the position of the argument in the array:


response 1

Here, index is the method and 'paper' the argument. If there is more than one argument, they can be passed to the method by simply separating them with commas.

check whether the number two is between one and three

2.between? 1,3

0.2 Syntactic Sugar for Special Methods

Special Methods

Integer objects list mathematical operators like + and - among their methods. Ruby as a language aims to be extremely programmer-friendly


1+2   # this is same as 1.+(2)


There are several other method names that have this special status

      • / = == != > < >= <= []

[] method

words = ["foo", "bar", "baz"]