0
0
Rubyprogramming~10 mins

Instance methods in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Agreet
Bgreeting
Csay_hello
Dhello
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than 'greet'.
Defining the method outside the class.
2fill in blank
medium

Complete 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'
Agreeting
Bsay_hello
Chello
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method name that does not exist.
Forgetting the dot before the method name.
3fill in blank
hard

Fix 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'
Aanimal_speak
BSpeak
Cspeak
Dspeak!
Attempts:
3 left
💡 Hint
Common Mistakes
Starting method name with uppercase letter.
Adding special characters to method names.
4fill in blank
hard

Fill 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'
Aage_in_dog_years
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Incorrect method name.
5fill in blank
hard

Complete 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'
Afull_name
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using + or - instead of space.
Forgetting the space between names.