In this Article We will learn about global variables
we will take another Class example. because we want to pursue Object Oriented Style of Coding.
Global Variables
Global variables in Ruby are accessible from anywhere in the Ruby program, regardless of where they are declared. Global variable names are prefixed with a dollar sign ($). For example:
$welcome = "Welcome to Ruby Essentials"
Now lets create a Class .. and use some global variables in it..
class Article
def initialize(*inputs)
@author = inputs[0]
@title = inputs[1]
@message = inputs[2]
@id = inputs[3]
end
def show_details
puts "#{@author} has posted an article, titled '#{@title}'"
puts "#{@message}"
end
end
print "Insert Author Name : "
$author_name = gets.chomp
print "Insert Title Post : "
$post_title = gets.chomp
print "Insert Message : "
$post_message = gets.chomp
my_article = Article.new $author_name, $post_title, $post_message
my_article.show_details
In this Example, We have created a Class called "Article" which we can use to create
articles for users.. note that our class has 4 properties.
- author
- title
- message
- id
These properties are held inside of Instance variables. @author, @title, @message and @id
This class also has 2 methods .. one is special initialize method, and second is regular method
which we can call to show object's information
def show_details
puts "#{@author} has posted an article, titled '#{@title}'"
puts "#{@message}"
end
Note these two ruby instructions
print "Insert Author Name : "
$author_name = gets.chomp
First instruction displays us a message on screen.
second instruction takes input from the user and stores the input value in global variable "$author_name"
Which we use later on to create an object of Article Class..
In this article we are taking Three input values from the user and then using these values to create an object from Article Class
Note this ruby instruction, where we used a local variable .. to store the object which is created by "Article.new"
my_article = Article.new $author_name, $post_title, $post_message
## which can also be written as
my_article = Article.new($author_name, $post_title, $post_message)
once our object is created we can then use it, to display the information it contains.
my_article.show_details
## which can also be called like so
my_article.show_details()
When we execute our program, it takes three input values. ..
the highlighted part is the result of above method call ..
Here is a list of some predefined Global variables that Ruby provides us..
These variables can be very useful when writing ruby programs in certain cases ..
We will talk about them in upcoming articles :) for now you can just try them.
Variable | What does it do? |
---|---|
$@ | The location of latest error |
$_ | The string last read by gets |
$. | The line number last read by interpreter |
$& | The string last matched by regexp |
$~ | The last regexp match, as an array of subexpressions |
$n | The nth subexpression in the last match (same as $~[n]) |
$= | The case-insensitivity flag |
$/ | The input record separator |
$\ | The output record separator |
$0 | The name of the ruby script file currently executing |
$* | The command line arguments used to invoke the script |
$$ | The Ruby interpreter's process ID |
$? | The exit status of last executed child process |
Key notes:
global variables start with $ symbol
local variables start with lower case letter , or _ understore
- _myarticle = Article.new("Bilal", "Awesome", "Good message")
We learned that object will still be created , if we supply 1, 2 or 3 out of 4 properties ..
- in our example we supplied only 3 values
we learned on how to take input from user, using "gets" Method which ruby provides us
- $input = gets , will take input from user and store it in $input variable.
- its not necessary to use global variables when taking inputs from user . (local variables are suggested)
- you can also do, _input = gets, or @input = gets .. you can use local variables, global variables, or instance variables
We learned to chomp the input, which removed the newline character from the input
We took a list of global variables that ruby language provide us.. and we can't wait to try them :)
We will continue, creating more classes in upcoming articles, and use different skills we learned so far ..
so that we can create beautiful ruby Classes and Objects ...
boss please vote me once let the spirit ... thank you
good post
Interesante!
Nice article. A lot of information is here. Thank you so much from PodinaTutorials.com