Ruby Flashcards
What is this operator < = > and what does it do?
The combined comparison operator (ie, Spaceship operator:
Returns 0 if the first operand equals the second operand;
Returns 1 if the first operand is greater than the second;
Returns -1 if the first operand is less than the second operand.
How would you compare and sort an array into an ascending order or items based on their size
arr.sort { |a ,b | a.size < = > b.size }
Use the following to sort in descending order
arr.sort { |a, b | a.size < = > b.size }
What method would we use to sort an array that has items with lower and upper case ie, normal spaceship sort comparison operator won’t work correctly.
arr.sort!{|a, b| a.casecmp(b)}
What is the notation to convert a string into an array
str = w% { convert a string into an array }
=> [“convert”, “a”, “string”, “into”, “an”, “array”]
What is the methods to add an item from the end of an array?
#push adds on the end of an array. It modifies the array or << shovel operator
arr = {1, 2, 3, 4]
arr.push( 555)
=> [1, 2, 3, 4, 555]
What is the method to remove an item from the end of an array?
pop removes the last item from an array. it modifies the array
arr= [1, 2, 3, 4, 555]
arr.pop
=>[1, 2, 3, 4]
What is the method to take off the first item from the following array?
arr = [1, 2, 3]
shift removes the first item from an array and modifies the array.
arr = [ 1, 2, 3]
arr.shift
=> [2, 3]
NB: #first gives the first item but doesn’t remove it ie, like arr [0]
How do you add an item onto the front of an array?
unshift adds an item onto the front of an arry and modifies the array
arr = [1, 2, 3]
arr.unshift (999)
=> [999, 1, 2, 3 ]
How do you delete an item from an array?
delete_at (index position)
arr = [ 1, 2, 3, 4 ]
delete_at ( 1 )
=> [ 1, 3, 4 ]
Note: if deleting items inside a loop it will change the index order.
How do you convert an Array into a String?
use #join and pass in anything you want in between each element (the “separator”)
[ “he”, “llo”]. join
=> [ “hello” ]
How do you break a string into pieces to create an array of substrings
Use #split
list = “eggs, milk, cheese and crackers”
list.split (“, “)
=> [“eggs”, “milk”, “cheese and crackers”]
list.split( “” ) or list.split ( // )
=> => [“e”, “g”, “g”, “s”, “,”, “ “, “m”, “i”, “l”, “k”, “,”, “ “, “c”, “h”, “e”, “e”, “s”, “e”, “ “, “a”, “n”, “d”, “ “, “c”, “r”, “a”, “c”, “k”, “e”, “r”, “s”]
How do you search within and replace items within strings?
Use RegExp # gsub(pattern, replace_with_this)
> “hello”.gsub( “l”, “r” )
=> “herro”
What methods could you use to iterate through a string and when would you use each method?
scan is used to iterate through a string matching the pattern (which may be a Regexp or a String). For each match, a result is generated and either added to the result array or passed to the block.
Use either #scan or #match
eg. Use a regex to match each individual character and return an array of the characters.
“some string”.scan( /. / )
=> [”s”, “o”, “m”, “e”, “ “, “s”, “t”, “r”, “i”, “n”, “g” ]
How do you create an object in Ruby? Give an example of the creation of an object.
class MyClass end
my_obj = MyClass.new
We crteate an object by defining a class and instantiating it using the .new method to create an instance, also known as an object.
What is a module and what is its purpose?
A module allows us to group reusable code into one place. We use modules in our classes by using the “include” reserve word, followed by the module name.
module Study end
class MyClass
include Study
end
my_obj = MyClass.new