Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define an instance method named greet inside the Person class.
Ruby
class Person def [1] puts "Hello!" end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than 'greet'.
Defining the method outside the class.
✗ Incorrect
The method name should be
greet as specified. This defines an instance method that can be called on Person objects.2fill in blank
mediumComplete the code to call the instance method greet on the person object.
Ruby
person = Person.new
person.[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method name that does not exist.
Forgetting the dot before the method name.
✗ Incorrect
To call the instance method
greet, use person.greet.3fill in blank
hardFix the error in the instance method definition by completing the method name correctly.
Ruby
class Animal def [1] puts "I am an animal" end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting method name with uppercase letter.
Adding special characters to method names.
✗ Incorrect
Method names in Ruby should start with a lowercase letter and
speak is the correct conventional name here.4fill in blank
hardFill both blanks to define an instance method age_in_dog_years that returns the age multiplied by 7.
Ruby
class Dog def [1] @age [2] 7 end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Incorrect method name.
✗ Incorrect
The method name is
age_in_dog_years and the multiplication operator * is used to multiply age by 7.5fill in blank
hardComplete the code to define an instance method full_name that returns the first and last name joined with a space.
Ruby
class User def [1] "#{@first_name} #{@last_name}" end end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
+ or - instead of space.Forgetting the space between names.
✗ Incorrect
The method
full_name returns the first and last name joined by a space using string interpolation.