How to Create a Class in Ruby

Ruby is a language built for defining classes, as it is first and foremost an object-oriented language. Ruby provides a number of shortcuts to create a class, which can be easily followed for those who have a bit of experience with the language.

Instructions

  1. Create a Class in Ruby

    • 1

      Choose a short and descriptive name for your class. The name should be concise enough that anyone reading your code can have a good idea of what the class does.

    • 2

      Use Camel case for the class name. Camel case uses a capital letter at the beginning of the class name and capitalize the first letter of the next word when joining words. This is instead of using an underscore. For example, Ruby method names look like this: method_name;but class names should look like this: ClassName.

    • 3

      Create an empty class statement:
      class TestClass
      end

    • 4

      Plan methods for for each thing your class should be able to do. Write, one by one, empty methods for each of the methods. A Microwave class, for example, should be able to do things like turn on and put food in it:
      class Microwave
      def put_food_in
      enddef take_food_out
      enddef turn_on
      enddef turn_off
      end
      end

    Write the Code for the Methods

    • 5

      Use the @variable syntax if the methods have to store a variable inside of an object. The "at" symbol basically says "This variable is not a local variable, it's a member variable. It belongs to the object." You can also use local variables in class methods. The example uses the @food member variable, wherein the variable stores what food is currently in the microwave:
      class Microwave
      def put_food_in(food)
      @food = food
      enddef take_food_out
      food = @food
      @food = nil
      return food
      enddef turn_on
      puts "Microwave is on"
      enddef turn_off
      puts "Microwave is off"
      end
      end

    • 6

      Begin a label with a ":" character and the name of the variable. For example, ":food". Here ":food" means "the thing called food." "@food" can't be passed to the attr_* methods, because that would pass the value of "@food". Since you essentially want to say "the thing called food can be accessed from outside the class," ":food" should be used.

    • 7

      Put one of the following three types of attr_* methods in your class, but outside of any methods. For example, attr_reader allows code outside the class to read the variable, but not write to it. On the other hand, attr_writer allows code outside the class to write to a variable, but not read it. Finally, attr_accessor allows both reading and writing.

    • 8

      Put one of the following three types of attr_* methods in your class, but outside of any methods:
      class Microwave
      attr_reader :fooddef put_food_in(food)
      @food = food
      end# ... the rest of the methods go here, taken out for brevity
      end

Tips & Warnings

  • If you're reading blocks of code, be sure you disregard anything written after the operator "#." These are notes designed to help the programmers. While these notes are beneficial when there is an entire team working on the same program, they can be easily confused with code block to the untrained eye.

Related Searches:

Comments

You May Also Like

  • How to Create an Array in Ruby

    There are three primary ways to create an array in Ruby: the array literal, building the array and returning an array. An...

  • How to Use Inheritance in Ruby

    Inheritance is the core of object oriented programming. Building class structures makes smaller programs easy, and large programs possible. In Ruby, inheritance...

  • How to Create a While Loop in Ruby

    When you create a while loop in Ruby, you're essentially saying "while X is true, do Y" or even "until X is...

  • How to Output a File in Ruby

    Ruby has a variety of ways which make it simple to output a file, depending on what you want to do with...

  • Ruby Bridges School Project Ideas

    Ruby Bridges became famous at the age of 6 in November 1960 when she, a young African-American girl, helped integrate the New...

  • How to Access the File Data in a Sequential Manner

    Files on disk are considered streams of bytes by the most popular operating systems, such as Microsoft Windows and variants of Unix,...

  • How to Create a for Statement in Ruby

    One of the things that makes Ruby preferable to programmers is that it's an object-based language. Programmers can create a for statement...

  • How to Create a Multidimensional Array in Ruby

    Though Ruby doesn't provide explicit support for multidimensional arrays, you can implement one yourself if you have a basic knowledge of the...

  • Java Exceptions Tutorial

    An exception occurs when an abnormal situation (such as an error or problem) occurs that a method is unable to handle. The...

  • What Is a CR Ruby?

    CR rubies are rubies that have the same natural contents and composition as rubies that are mined, but CR rubies are created...

  • How to Input a File in Ruby

    Reading data from and writing data to a file are common tasks in programming. The programming language Ruby has a number of...

  • How to Make Ruby Slippers for a Dorothy Costume

    Dorothy from “The Wizard of Oz” is a popular Halloween costume. While many shops and websites offer Dorothy costumes in various styles...

  • How to Upgrade Ruby Gems

    Ruby is an open source, object oriented programming language. By creating different objects, called Gems, programmers can assemble software packages that are...

  • How to Build Ruby Gems

    Ruby is a programming language created by Japanese computer scientist Yukihiro Matsumoto. Ruby libraries are distributed using a centralized network and package...

  • How to Write a Simple Program in Ruby

    Ruby is an extremely powerful programming/scripting language. With powerful capabilities and an efficient syntax, many things are possible with Ruby. This article...

  • How to Make a Class Schedule

    Making a class schedule helps students prepare for classes ahead of time. Teachers can print out class schedules for their younger students,...

  • How to Make Easy Money in Pokemon Ruby

    Capturing Pokemon in Ruby Version and working to be the best Pokemon trainer can be an expensive journey. You mother knows that...

  • How to Make Friends in Class

    School is more than a place where students learn predetermined subjects such as math, science and reading---it is also an environment that...

  • How to Write a Class President Speech

    If you are running for class president, you will need to write a speech to present to the student body. There will...

Related Ads

Featured