Conventions Flashcards
Which case/format should Classes be in?
CamelCased
Print all the words in docs.words
docs.words.each { |word| puts word }
Which case/format should methods, arguments and variables be in?
snake_cased
What is a better way to write the following:
if not @read_only
unless @read_only
What is a better way to write the following:
while ! document_is_printed?
until document.printed?
One liner to conditionally set @title to new_title only if document.read_only? is not true from inside the document.
@title = new_title unless read_only?
How does ruby treat booleans?
Only false or nil are treated as false.
Everything else is true.
How would you initialize a variable only if the variable is nil?
@variable ||= ‘’
What is a shortcut for creating an array of strings?
array_of_strings = %w{California Florida Ohio} #=> ["California", "Florida", "Ohio"]
How can you create a hash? Give 2 ways.
- { first_name: ‘Russ’, last_name: ‘Olsen’ }
2. Hash.new(first_name: ‘Russ’, last_name: ‘Olsen’)
How would you make a method that takes an arbitrary set of arguments?
def some_method( args )
How do multiple arguments get parsed when using a splat?
They are passed in as an array
What is the shortcut for passing a hash as an argument?
load_font( :name => ‘times roman’, :size => 12)
or
load_font :name => ‘times roman’, :size => 12
How would you use an each block to access data in a hash?
movies.each { |name, value| puts “#{name} => #{value}” }
How is .map different from .each?
.map creates a new array containing everything that the block returns
It is useful for transforming the contents of a collection en masse