Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Dog # define a class named dog
- attr_accessor :name # define a writer and reader method to access @name instance variable
- def initialize(name) # define the class initializer, new method calls this under the hood
- @name = name # Assign the name argumant to @name instance variable
- end
- def bark # defines a bark method
- "#{@name} barks" # interpolates the @name instance variable into the string
- end
- end
- dog = Dog.new("Larry") # Initializing a new class of dog with larry as the name argument
- dog.name = "Harry" # Using the attr writer to asign anew value to name
- dog.name # using the reader to show the value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement