Request Dev Services » / 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.


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

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

One ="1", "1", "1")
Two ="2", "4", "8")
Five ="5", "25", "125")
Ten ="10", "100", "1000")
Twenty ="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