Ryan Ho

Think, Create, Do, Repeat

LinkedIn GitHub Facebook Twitter

Ruby Classes

November 5, 2015 | Coding

Creating a Dog Class

Here's how we can create a Dog Class with several characteristics like introduce and bark:


	    		class Dog 
	    			def initialize(name)
	    				@name = name
	    			end

	    			def introduce
	    				puts "Hello everyone, my name is #{@name}"
	    			end

	    			def bark
	    				puts "Woof woof! Go away!"
	    			end
	    		end
	    		

To create an instance of the Dog Class, we can do something like this:


	    		truffles = Dog.new("Truffles")
	    		

To tell Truffles to introduce himself and bark, we can do something like this:


	    		truffles.introduce
	    		truffles.bark
	    		

Comments box

Name:
Email:
Comments: