Ruby Intro - General Flashcards

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

What do you call a method with exclamation point? 2 answers.

A

Bang version of the method or unsafe method.

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

What is a module and give an example.

A

A module is a collection of methods (and constants). Modules can be mixed in to other classes; this just means that the module’s methods are available in the class that it is mixed into.

Enumerable is one of the most powerful modules because it holds Each and Map. It is also mixed into other classes like Arrays and Hash, it’s also available to those classes as well.

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

What is difference between gsub and sub?

A

gsub is “general” sub and replaces all instances. sub only replaces first instance.

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

What is difference between inspect and to_s?

A

Inspect returns valid ruby code for debugging purposes (always surrounded by quotes). to_s transforms to string and is used as part of program (sometimes in quotes).

[1, 2, 3].to_s # => “[1, 2, 3]”
“my_string”.to_s # => “my_string” (already a string)
nil.to_s # => “” (nil represented as nothing, or emptiness)

[1, 2, 3].inspect # => “[1, 2, 3]” (same as to_s)
“my_string”.inspect # => ‘“my_string”’ (notice the quotes)
nil.inspect # => “nil”

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

What is the difference between puts and p?

A

Puts is like to_s. It prints out the string. ‘p’ is like inspect. It prints out the ruby code for debugging purposes (always in quotes).

puts “my string!” # prints “my string!”
p “my string!” # prints ‘“my string!”’

puts nil # => prints a blank line
p nil # => prints “nil”

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

What is a Kernel module?

A

Methods that are defined at the top level scope are methods of the Kernel module. The Kernel module is mixed into every class, so that you may call these “global” methods from any context. Puts and gets are kernel.

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

How do you use nil?

A

puts “Couldn’t find answer” if [1, 2, 3].index(42).nil?

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

How do you use class method?

A

Tells you what kind of class an object is:

“string”.class # => String
3.class # => Fixnum

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

When creating a method, how do you define a default parameter (when none is received)?

A

def h(name = “default”)

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

What is general rule for nesting loops?

A

Nesting 2 loops is ok. But more is generally bad. You should create methods if you need to have deeply nested loops.

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

A good method has what 3 characteristics?

A

does one thing, short descriptive name, <10 lines of code

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

How do you do a conditional assignment (that is, assign value to a variable only if it is empty)

A

array ||= [1]
is the same as
array = [1] if array.nil?

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

How is “return” treated different in Ruby methods as opposed to Javascript functions?

A

In Ruby, return is implicit, meaning it’s not necessary to type in. In Javascript, you must type in “return” otherwise you will get an “undefined.” In Python, no “return” returns “None.”

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

How do you use the “times” method? Sample code.

A

3.times {code here}

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

What two common looping methods are not used much in Ruby? And what are their better alternatives?

A

For..in and Loop… methods.

The For..in loop is better served in Ruby by the .times or .each or .upto/.downto methods

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

What is the &laquo_space;method a shorthand substitute called? What’s it used for? And what objects can it be used on?

A
  1. Called the shovel
  2. short for .push and/or .concatenate
  3. Can be used on strings and arrays
17
Q

How do you use ternary conditional that outputs “true” or “false”? Example code.

A

puts 1 > 0 ? “True” : “False”

18
Q

what can for in (1..8) method be replaced with?

A

8.times method

19
Q

What are splats?

A

Splat arguments are arguments preceded by a *, which signals to Ruby: “Hey Ruby, I don’t know how many arguments there are about to be, but it could be more than one.”

20
Q

What is a block? How are they defined?

A

Blocks are like methods without names. They are like anonymous functions in Javascript (or lambdas in Python). They are defined by “do…end” or “{ ..}”

21
Q

What’s the combined comparison operator? What’s it used for?

A

. Used for sorting algorithm

22
Q

Mnemonic for remembering the output for comparison operator?

A

In a game of “who’s the biggest”, the numbers are in reverse order. 1,0,-1. If left side is biggest, output is 1. If smaller, output is -1. OR, just remember the default ab outputs -1. and -1 is the boolean false.

23
Q

Why can’t blocks be saved to variables?

A

Because blocks are not objects (they are one of few exceptions to Object rule in Ruby). This is why we use procs. Procs can be saved to variables.

24
Q

What is a proc? What purpose do procs serve?

A

A “saved” block. This allows you to use a block multiple ties.

25
Q

what is a module?

A

It is a toolbox that holds a set of methods and contants