Classes and Objects
Classes are abstract templates for objects. You can also say that objects are instances of classes. Classes contain the template for a set of behaviors (such as methods) and data (such as variables). As mentioned in the preceding example, at our company, we want to let everyone vote, so we need a way to keep track of those users and we need to model each user's behavior, such as the ability to vote. Here is an example of how we can create a User
class to do this:
class User def initialize(name) @name = name end end
We've created the base template for a user by using the class
keyword and have given it the name User
. Based on what we've learned so far, we can tell right off the bat that we have a User
class with a method called initialize
, which takes an argument of name
. The body of the User
class is concluded as usual with an end
keyword. Additionally, we should note that the name of the User
...