0
0
Rubyprogramming~10 mins

Self keyword behavior 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 print the current object using self.

Ruby
puts [1]
Drag options to blanks, or click blank then click option'
Aself
Bthis
Cme
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' or 'me' which are not Ruby keywords.
Trying to print an undefined variable instead of self.
2fill in blank
medium

Complete the method to return the class of the current object using self.

Ruby
def get_class
  [1].class
end
Drag options to blanks, or click blank then click option'
Athis
Bself
Cobject
Dme
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' or 'me' which are not valid in Ruby.
Calling class on an undefined variable.
3fill in blank
hard

Fix the error by replacing the blank with the correct keyword to call a method on the current object.

Ruby
def greet
  [1].say_hello
end
Drag options to blanks, or click blank then click option'
Aobject
Bthis
Cme
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' or 'me' which cause errors.
Omitting the receiver when explicit self is needed.
4fill in blank
hard

Fill the blank to define a class method using self.

Ruby
class Person
  def [1].greet
    puts "Hello!"
  end
end
Drag options to blanks, or click blank then click option'
Athis
Bme
Cself
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance method syntax instead of class method syntax.
Using invalid keywords like 'this' or 'me'.
5fill in blank
hard

Fill all three blanks to complete the code that prints the object id using self inside an instance method.

Ruby
class Item
  def print_id
    id = [1].[2]
    puts "ID: [3]"
  end
end
Drag options to blanks, or click blank then click option'
Aself
Bobject_id
C#{id}
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' instead of 'self'.
Forgetting to use string interpolation to print the variable.