Variables & Methods Flashcards
How do you define a variable?
variable_name = value
How do I reassign the value of:
variable_name = value
variable_name = other_value
What does snake case look like?
this_is_snake_case
What does camel case look like?
thisIsCamelCase
True or False: Variables can be written in both snake case and in camel case.
True
True or False: Variable names should start with a lowercase letter.
True
True or False: This is an acceptable name for a variable: 1st_place
False
Variable names should never start with a number.
True or False: This is an acceptable name for a variable: end
False
end is a reserved word in Ruby, meaning that it is used for other purposes and cannot be used as a variable name.
True or False: This is an acceptable name for a variable: danny’s_age
False
Apostrophes are not allowed in variable names.
name=”Richard”
How do I output the following using string interpolation?
“Hello, my name is Richard!”
puts “Hello, my name is #{name}!”
What is the difference between puts and print?
puts will output your code and will enter a new line. print will output your code but will not enter a new line.
How would you define a method named greeting?
def greeting #your code here end
How would you define a method named greeting with the argument of name?
def greeting(name) #your code here end
How do you invoke a method?
greeting(“Richard”)
True or False: A method can have multiple arguments.
True
Ex:
def greeting(name, date)
end
How do you write an optional argument in a method?
def greeting(name="Ruby programmer") #your code here end
What does an optional argument do?
If a method is called and no argument is given, it will default to the default value given. This is good when you want to avoid errors. Ex: def greeting(name="Ruby programmer") puts "Hello, #{name}!" end
greeting("Richard") #Will output "Hello, Richard!"
greeting() #Will output "Hello, Ruby programmer!"
True or False: You can use both optional and required arguments for the same method.
True
Ex:
def greeting(name, age=”a secret”)
end
True or False: It is okay to define optional arguments before required arguments in a method.
False
Ex:
def greeting (name=”Ruby programmer”, age)
puts “Hello, my name is #{name} and my age is #{age}!”
end
#greeting(24)
#This will error out since the method will think name is 24 and age is not defined
def greeting(age, name="Ruby programmer") puts "Hello, my name is #{name} and my age is #{age}!" end #greeting(24) #Will come out as "Hello, my name is Ruby programmer and my age is 24!" #Should be written like this
How is return different from puts/print?
puts/print can show you the value but return will actually give you the value.
Ex:
#puts/print
def greeting
puts “Hello there!”
end
#Will output “Hello there!” but value will be nil
#return def greeting "Hello there!" end #Value will be "Hello there!"