Types of Variables in Rhomobile

Variable is a symbol or name that stands for a value. Variables play an important role in computer programming because they enable programmers to write flexible programs. Rather than entering data directly into a program, a programmer can use variables to represent the data. Later, when the program is executed, the variables are replaced with real data. This makes it possible for the same program to process different sets of data.

There are five types of variables supported in Rhomobile. These types of variables are explained below.

1. Global Variables:-

Global Variables are denoted with $. Uninitialized global variables have the value nil.

You can use the global variable in application.rb to make this accessible throughout the application.

# Declaring global variables
$server_url ||= “http://some_domain.com/api/”

This variable can be used in any of the model controller and also can be used in the views also.

2. Local Variables:-

Local Variables begins with a lowercase letter or _ (underscore). This has a scope to its respective block or you can say its scope starts from the blocks’ opening brace ({) to the closing brace (}).

def test_method
# Local variable declaration
# this variable can be accessed within this method
server_api = “test_api/upload/particle”
end

3. Class Variables:-

Class Variables are denoted with @@ and should be initialized before they can be used in any method. Referencing an uninitialized class variable produces an error. Class variables can only be accessed in that class and within all the methods associated with the class. This variable can’t be accessed in the views.

class Person

# Global variable declaration
@@first_name = “Bruce”
@@last_name = “Wayne”
@@popular_name = “Batman”

# Method 1

def whoami
p “I am #{@@first_name} #{@@last_name}.”
end

# Method 2

def whatami
p “I am #{@@first_name} #{@@last_name}.”
end
end

4. Instance Variables:-

Instance Variables are denoted with @. Uninitialized instance variable refers to nil. Scope of the instance variables are within that method in the controller, and can also be used in its respective views/partials.

class Person

# Method 1

def whoami
@first_name = “Bruce”
@last_name = “Wayne”
end
end

In View, it can be accessed as like below,
<p>I am <%= @ first_name %> <%= @ last_name %>.</p>

5. Pseudo Variables:-

Pseudo variables are special variables that have the appearance of local variables but behave like constants. You cannot assign any value to these variables.

Some of them are

SELF – object referring to the current method
TRUE– refers to the boolean true
FALSE– refers to the boolean false
NIL– refers to undefined

That’s all, these are the defined types of variables in Rhomobile.

Written By: Ashis Kumar, Software Developer, Mindfire Solutions

Advertisements

One thought on “Types of Variables in Rhomobile

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s