Complete the code to make the method private.
class Person def initialize(name) @name = name end [1] def secret "My name is #{@name}" end end
The private keyword makes the method accessible only within the class.
Complete the code to make the method accessible to instances of the same class and subclasses.
class Person def initialize(name) @name = name end [1] def greet(other) "Hello, #{other.name}!" end attr_reader :name end
The protected keyword allows method access within the class and its subclasses, including other instances.
Fix the error in the code by choosing the correct visibility keyword for the method.
class BankAccount def initialize(balance) @balance = balance end [1] def display_balance "Balance: $#{@balance}" end end account = BankAccount.new(1000) puts account.display_balance
The method must be public to be called from outside the class instance.
Fill both blanks to define a protected method and call it from another instance of the same class.
class User def initialize(name) @name = name end [1] def secret_message(other) "Secret for #{other.name}" end def reveal_secret(other) other.[2](self) end attr_reader :name end
The method secret_message is marked protected and called on another instance.
Fill both blanks to define a private method and call it correctly inside the class.
class SecretAgent def initialize(code_name) @code_name = code_name end def reveal [1] end [2] def code "Agent: #{@code_name}" end end
The private method code is called without an explicit receiver inside the class. The method is marked private. The reveal method is public by default.