Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. class Dog # define a class named dog
  2. attr_accessor :name # define a writer and reader method to access @name instance variable
  3.  
  4. def initialize(name) # define the class initializer, new method calls this under the hood
  5. @name = name # Assign the name argumant to @name instance variable
  6. end
  7.  
  8. def bark # defines a bark method
  9. "#{@name} barks" # interpolates the @name instance variable into the string
  10. end
  11. end
  12.  
  13. dog = Dog.new("Larry") # Initializing a new class of dog with larry as the name argument
  14. dog.name = "Harry" # Using the attr writer to asign anew value to name
  15. dog.name # using the reader to show the value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement