Variables in Ruby

ruby 34 Comments »

Declaring variables in Ruby is easy. Let’s start by giving the string and integer example.

# variable example
variable = 'string'; #string
number = 10; #integer

Wasn’t that easy. Try running this program:

#variable in output example
name = "adriaan";
print "My name is " + name + ".\n";

There are more sorts of variables in Ruby. There are constants, global variables, class variables and instance variables.

Constants are always uppercase.

PI = 3.141592654;
print PI;
 
#output 3.141592654

If you put $ before the variable. You declare the variable a global variable.

$global = "global variable";

Instance variables are created by putting @ in front of the variable. These variables are an instance of the variable ’self’. They belong to the object itself.

@variable

An example of a class variable:

@@variable

Playing with Ruby

ruby 9 Comments »

Ruby is an object orientated programming language from Japan. It’s fairly easy to use and comes with the Rails framework.

I thought I’d give a try to check out Ruby.

You can download ruby here.

I’ve also downloaded Aptana for Ruby. Aptana is a very handy program for editing HTML, CSS and Javascript. You can also install plugins for it, like an extension for PHP and Ruby.

The hello world application.

1
puts "Hello World";

However, you can also use ‘print’. Instead of puts. The difference is that puts adds a newline.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in