Conventions Flashcards

1
Q

Which case/format should Classes be in?

A

CamelCased

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

Print all the words in docs.words

A

docs.words.each { |word| puts word }

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

Which case/format should methods, arguments and variables be in?

A

snake_cased

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

What is a better way to write the following:

if not @read_only

A

unless @read_only

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

What is a better way to write the following:

while ! document_is_printed?

A

until document.printed?

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

One liner to conditionally set @title to new_title only if document.read_only? is not true from inside the document.

A

@title = new_title unless read_only?

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

How does ruby treat booleans?

A

Only false or nil are treated as false.

Everything else is true.

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

How would you initialize a variable only if the variable is nil?

A

@variable ||= ‘’

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

What is a shortcut for creating an array of strings?

A
array_of_strings = %w{California Florida Ohio}
#=> ["California", "Florida", "Ohio"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you create a hash? Give 2 ways.

A
  1. { first_name: ‘Russ’, last_name: ‘Olsen’ }

2. Hash.new(first_name: ‘Russ’, last_name: ‘Olsen’)

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

How would you make a method that takes an arbitrary set of arguments?

A

def some_method( args )

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

How do multiple arguments get parsed when using a splat?

A

They are passed in as an array

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

What is the shortcut for passing a hash as an argument?

A

load_font( :name => ‘times roman’, :size => 12)

or

load_font :name => ‘times roman’, :size => 12

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

How would you use an each block to access data in a hash?

A

movies.each { |name, value| puts “#{name} => #{value}” }

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

How is .map different from .each?

A

.map creates a new array containing everything that the block returns

It is useful for transforming the contents of a collection en masse

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

What does .inject do?

A

Every time it calls the block, it replaces the current result with the return value of the previous call to the block. It returns the result as its return value when it runs out of elements.

17
Q

How would you run a method in place on an object rather than returning a new object? (For many methods, not all)

A

Add a bang to the method (Example: a.reverse! )