Ruby Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is this operator < = > and what does it do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How would you compare and sort an array into an ascending order or items based on their size

A

arr.sort { |a ,b | a.size < = > b.size }

Use the following to sort in descending order

arr.sort { |a, b | a.size < = > b.size }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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.

A

arr.sort!{|a, b| a.casecmp(b)}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the notation to convert a string into an array

A

str = w% { convert a string into an array }

=> [“convert”, “a”, “string”, “into”, “an”, “array”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the methods to add an item from the end of an array?

A
#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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the method to remove an item from the end of an array?

A

pop removes the last item from an array. it modifies the array

arr= [1, 2, 3, 4, 555]
arr.pop

=>[1, 2, 3, 4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the method to take off the first item from the following array?

arr = [1, 2, 3]

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you add an item onto the front of an array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you delete an item from an array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you convert an Array into a String?

A

use #join and pass in anything you want in between each element (the “separator”)

[ “he”, “llo”]. join

=> [ “hello” ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you break a string into pieces to create an array of substrings

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you search within and replace items within strings?

A

Use RegExp # gsub(pattern, replace_with_this)

> “hello”.gsub( “l”, “r” )

=> “herro”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What methods could you use to iterate through a string and when would you use each method?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you create an object in Ruby? Give an example of the creation of an object.

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a module and what is its purpose?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are getters & setters?

Write out the getter & setter methods based off this shortcut version

attr_accessor :name, :height

A

Getters allow us to retrieve the state of an object.
Setters allow us to change the state of an object

def name
@name
end

def name=(new_name)
  @name = new_name
end

def height
@height
end

def height=(new_height)
  @height = new_height
end