Handy Advanced Commands Flashcards
1
Q
In a class called Person, how could you make it so, that on an instance of the class it will raise an error if no name is filled in on initialize?
A
class Person attr_reader :name
def initialize(attrs = {})
set_name(attrs[:name])
end
private
def set_name(obj) obj == nil ? missing_name : @name = obj end
2
Q
With the following date assigned to num, convert it to a date, and have num = the current date in string from to what it currently is plus one month. Do the same so the year is changed to 5 year ahead
A
num = ‘11/17’
num = Date.today.next_month(1).strftime(‘%m/%y’)
or
num = Date.today.next_year(5).strftime(‘%m/%y’)
3
Q
Write out the current date and make sure that 20 is printed in front of the year.
A
num = Date.today.next_year(5).strftime(‘%Y-%m-%d’)