Ruby Classes

6.23.2015

Class person def initalize Name age height weight @name=Name @age=age @height=height @weight=weight end def SayHi puts "Hi my name is #{@name}" end def giveInfo puts "I am #{@age}" years old puts "I weigh #{@weight} pounds" puts "I am #{height}" end end

Classes in Ruby are used to group common information, methods, and functionality. An example class is shown above; it describes a person by their name, age, height, and weight and also includes some methods that would be what you want your "person" to be able to do. Classes in Ruby are on of the most fundamental building blocks of the language. How fundamental? Think of the "strings", "arrays", "integers" that you create to put your variables in. Guess how Ruby groups all the common characteristics, functionality, and interactions of those objects? Yup, classes. Indeed it makes perfect sense that while you may know how to manipulate the behavior of any string, array, or integer, behind the scenes what stores that information is the class definition of that object. Ruby even gives you the ability to overwrite any class definition if you so desire, which can be useful, but also dangerous.

Just like the instances of string, integer, and array that are used to store variables, in order to use the methods of class "person" you must instantiate an "person" object.