Rubymonk
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}"
end
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'
Find if this contain letter R
"I am a Rubyist".index('R')
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
Methods that the string object provide fro string manipulation
puts 'i am in lowercase'.upcase #=> 'I AM IN LOWERCASE'
Two ways to do the same thing
'Ruby' + 'Monk'
The literal and expressive method
"Ruby".concat("Monk")
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')
'RubyMonk'.gsub(/[aeiou]/,'1')
Could you replace all the characters in capital case with number '0'
'RubyMonk Is Pretty Brilliant'.gsub(/[A-Z]/,'0')
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)
Methods that the string object provide fro string manipulation
puts 'i am in lowercase'.upcase #=> 'I AM IN LOWERCASE'
In Ruby, just like in real life, our world is filled with objects. Everything is an object - integers, characters, text, arrays
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
2.even?
Invoking a method on an object generate a response (2.even? true )
1.next.next
Call the method twice
Ruby objects are happy to tell you what methods they provide. You simply call the methods method on them.
1.methods.sort
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
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:
['rock','paper','scissors'].index('paper')
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
Integer objects list mathematical operators like + and - among their methods. Ruby as a language aims to be extremely programmer-friendly
4.+(3)
1+2 # this is same as 1.+(2)
7
There are several other method names that have this special status
[] method
words = ["foo", "bar", "baz"]
words[1]
bar