Intro to Ruby Flashcards

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

What’s an exception to the ‘everything is an object’ rule in Ruby?

A

Blocks are not objects, and this is one of the very few exceptions to the “everything is an object” rule in Ruby.

Because of this, blocks can’t be saved to variables and don’t have all the powers and abilities of a real object. For that, we’ll need… procs!

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

What is a Proc? And how do you define one?

After you’ve defined it, how do you use it?

A

A Proc is essentially a block that has been assigned to a variable.

To define a Proc, you can do the following:
variable_name = Proc.new {|x| x*2}

You can then pass this Proc to a method, but you’ll need to add an & to convert it back to a block before the method will accept it. For example:

other_variable.collect(&variable_name)

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

What’s the difference between a PROC and a lambda?

A

First, a lambda checks the number of arguments passed to it, while a proc does not. This means that a lambda will throw an error if you pass it the wrong number of arguments, whereas a proc will ignore unexpected arguments and assign nil to any that are missing.

Second, when a lambda returns, it passes control back to the calling method; when a proc returns, it does so immediately, without going back to the calling method.

def batman_ironman_proc
  victor = Proc.new { return "Batman will win!" }
  victor.call
  "Iron Man will win!"
end

puts batman_ironman_proc

def batman_ironman_lambda
  victor = lambda { return "Batman will win!" }
  victor.call
  "Iron Man will win!"
end

puts batman_ironman_lambda

See how the proc says Batman will win? This is because it returns immediately, without going back to the batman_ironman_proc method.

Our lambda, however, goes back into the method after being called, so the method returns the last code it evaluates: “Iron Man will win!”

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

What’s the difference between a Proc, A Lambda and a block?

A

A block is just a bit of code between do..end or {}. It’s not an object on its own, but it can be passed to methods like .each or .select.

A proc is a saved block we can use over and over.

A lambda is just like a proc, only it cares about the number of arguments it gets and it returns to its calling method rather than returning immediately.

There are obviously lots of cases in which blocks, procs, and lambdas can do similar work, but the exact circumstances of your program will help you decide which one you want to use.

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

What does ‘puts’ actually do?

A

Before ‘puts’ tries to write out an object, it uses the to_s to get the string version of that object. In fact, the s in puts stands for string; puts really means ‘put string’

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

What does ‘chomp’ actually do?

A

It takes off any trailing ‘enter’ characters hanging out at the end of the string you enter.

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

What are the 2 different classes of integers in Ruby?

A

Fixnum and Bignum

Really big integers are BigNum, and smaller ones are FixNum.

Most programming languages don’t have the concept of a Bignum

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

Why does ‘pig’ * 5 work but 5 * ‘pig’ does not?

A

in ‘pig’ * 5, you’re asking pig to do the multiplying. ‘pig’ knows how to make 5 copies of itself.

However, 5 doesn’t know how to make ‘pig’ copies of itself!

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

Do methods like .upcase or .capitalize change the variable they’re called upon?

A

No - the original variable remains unchanged.

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

What’s the difference between using single quotes or double quotes in ruby?

A

With double quotes you can easily do string interpolation using the #{variable_name} way

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

What’s a ruby symbol? How do you use it?

A

Ruby symbols are created by placing a colon (:) before a word.

Examples of symbols
:name
:a_symbol
:“surprisingly, this is also a symbol”

Basically, a symbol is used when you want to reference something like a string but don’t ever intend to print it to the screen or change it. It is often referred to as an immutable (i.e. unchangeable) string.

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

What is nil? What’s it used for?

A

In programming, we need a way to express “nothing”, and in Ruby, we do this through something called nil. A variable with a value of nil could be described as having ‘nothing’ or being ‘completely empty’, or even just simply ‘not any specific type’. A situation where this may occur is where output is expected but none is returned, such as:

irb :001 > puts “Hello, World!”
Hello, World!
=> nil

The puts method prints out a string and returns nothing, so we see nil being returned after the string is displayed.

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

When doing string comparison, what do you need to watch out for?

What will ‘bug lady’ < ‘Xander’ return, true or false?

A

It will return false.

When Ruby compares strings, it does so on their lexicographical ordering - but it goes over the capital letters first before moving on to the other letters.

So if you wanna do a proper comparison, you have to compare the .downcase version of each word.

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

What is a clasS?

A

A class is a another word for a ‘kind’ of object. Examples are strings, integers, floats, arrays, hashes, etc. They are always capitalized (String, Integer, Float, etc)

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

What is the difference between new and initialize

A

The methods new and initialize work hand in hand.

You use new to create a new object, and initialize is called automatically (if you’ve defined it in your class).

First, new is a method of the class, while initialize is a method of the instance. That means that new needs to be called first. Otherwise, there is no instance to initialize upon.

Second, you define initialize in your class, but you don’t need to define new (it’s already built into to all the classes). Conversly, you call new to create an object, but you never call initialize (the method new already takes care of that for you).

The reason for having these 2 methods is that you really need one of them to be a class method and the other one to be an instance method.

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

What’s the difference between ‘p’ and ‘puts’ in terms of what they return?

A

‘puts’ puts out whatever expression you pass it, but it returns nil

p puts out whatever expression you pass it AND it returns the value of that expression

(PS: print also returns nil - it behaves just like puts but there’s no newline \n character)

17
Q

With loops, what’s the difference between ‘break’ and ‘exit’ ?

A

The break keyword allows us to exit a loop at any point, so any code after a break will not be executed. Note that break will not exit the program, but only exit the loop and execution will continue on from after the loop.

exit, however, completely exits the program

18
Q

What is a block?

What are the styles to define a block?

A

A block is just some lines of code ready to be executed. When working with blocks there are two styles you need to be aware of. By convention, we use the curly braces ({}) when everything can be contained in one line. We use the words do and end when we are performing multi-line operations.

19
Q

Difference between .map and .each methods

A

.each just iterates over the array (and executes the block on each element), eventually returns the original array.

.map does the same thing, but returns a ‘new’ array with the modified elements. However, important to note that neither .map and .each mutate the caller, so the original array doesn’t change.

20
Q

What is the bang operator and how can we use it?

A

The bang operator (!) at the end of the method name usually indicates that the method will change (or mutate) the caller permanently. Unfortunately this is not always the case. It is a good rule to be wary of any method that has the bang operator and to make sure to check the Ruby documentation to see if it will behave destructively (the word “destructive” here just means mutating the caller).

21
Q

What is the .unshift method?

A

The unshift method adds the arguments that you specify to the front of the array.