Request Dev Services »

cdev.co / Notes /

Objects, classes, and puts (intro ruby tips)

The below is a simple program with ruby that is basically creating an object class and then declaring a few different object with different object properties and variables associated within.

Here is the object class, we are essentially defining a blank template of sorts to run through our later declared objects  property variables; here is the object class name and it’s parameters.

#!/usr/bin/ruby

class Numbers
  def initialize(id, squared, cubed)
    @id     = id
    @squared   = squared
    @cubed = cubed  
  end
end

Now, we’ll use it or ‘define it’ as such —

One = Numbers.new("1", "1", "1")
Two = Numbers.new("2", "4", "8")
Five = Numbers.new("5", "25", "125")
Ten = Numbers.new("10", "100", "1000")
Twenty = Numbers.new("20", "400", "8000")

Now, let’s write something so that it outputs to the terminal so we can test it easily. And for fun, let’s add a sleeper delay and a refresh —

We are using puts to output, and Object.inspect

puts One.inspect
sleep 1
system ("cls")

puts Two.inspect
sleep 1
system ("cls")

puts Five.inspect
sleep 1
system ("cls")

puts Ten.inspect
sleep 1
system ("cls")

puts Twenty.inspect
sleep 1
system ("cls")

Now, just simply run your file from your terminal and watch the output. ie. rubyfilename.rb

About the Author
Cameron Cashwell Web Developer
I build websites, web apps, and software. Wanna work together? Let's chat about your project!

Leave a Reply

Be the First to Comment!

Notify of
avatar