官术网_书友最值得收藏!

Variables and types

If you are new to programming, then just for a quick reference: a variable is nothing, but a memory location where we can save some data. This memory location can be easily referenced by a name and that name is called the variable name. Many languages such as C, Java, and so on, force you to declare variables before you use them, and they also expect you to specify the type associated with the variable. For example. if you want to store a number in the memory in a C program, you'd say the following:

int x = 10;

This will create a 2 byte memory space and that memory space will contain a binary associated with 10. The memory location where 10 is stored can be referenced through a variable name called x.

Ruby, on the other hand, is pretty lenient and it doesn't expect you to specify data type associated with a variable. Ruby, hence, belongs to a family of programming languages that are called dynamically typed languages. Unlike strongly typed languages, dynamic languages do not declare a variable to be of certain type; instead, the interpreter determines the data type at the time some value is assigned to the variable:

x = 10
puts x
x = "Hello"
puts x

The preceding piece of code, when executed, will give the following output:

10
Hello

Variable names in Ruby can be created using alphanumeric characters and underscores. A variable cannot begin with a number. Variable names cannot begin with a capital letter, as the Ruby interpreter will consider it to be a constant.

Variable names can also start with special characters called sigils. A sigil is a symbol that is attached to an identifier (this is just the name of the variable, method, or class). We'll see more about methods and classes later in this chapter. Sigils in Ruby are used to determine the scope of variables (scope is the range in your code where the variable can be referenced). We've the following sigils available in Ruby:

  • $
  • @

Any variable without a sigil is a local variable. This variable's scope is limited to a block, method, or class in which it's defined.

Global variables begin with the $ sign. They are visible everywhere across your program. Though they seem like a good feature, the use of global variables should be avoided in general.

Any variable starting with the @ character is an instance variable. Instance variables are valid inside an object.

Finally, any variable with the @@ character as prefix is called a class variable. This variable is valid for all instances of a class.

There are a few methods provided by Ruby that will help you find local_variables, global_variables, instance_variables, and class_variables in a given context of the code. Let's see them in action:

#!/usr/bin/env ruby
w = 10
$x = 20
@y = 30
@@z = 40
p local_variables
p global_variables.include? :$x
p self.instance_variables
p Object.class_variables
Note

The p method like puts can be used to display output. However, there is a subtle difference between the two. The puts method calls the to_s method on the object and returns a readable version of the object. The p method, on the other hand, calls the inspect method instead of to_s. The p method is better suited for debugging.

The local_variables method gives us an array of all the local variables defined in a specific context. The global_variables method produces a list of all global variables. Since there are a lot of default global variables created by Ruby and we didn't want to list them all, we chose the include method of the Array class, which tells us whether something belongs to the concerned array or not. The self pseudo variable points to receiver of the instance_variables method. The receiver in our case is the main method. Finally, we have an array of class variables associated with Object. The main is an instance of the Object class.

Let's run the code and see what happens:

$./sigils.rb
[:w]
true
[:@y]
[:@@z]

Often we need to declare some variables at the operating systems' environment level, and we might need to make use of them. The ENV constant gives access to environment variables. This is a Ruby hash. Each environment variable is a key to the ENV hash. The ARGV constant, on the other hand, holds command-line argument values. They are passed by a programmer when a script is launched. The ARGV array is an array that stores arguments as strings. The $* is an alias for ARGV.

Ruby also has some predefined variables such as $0, $*, and $$. The $0 variable stores the current script name. The $* variable stores command-line arguments. The $$ variable stores the PID of the script.

主站蜘蛛池模板: 从化市| 盐城市| 梅州市| 湖南省| 聂荣县| 新野县| 中西区| 惠水县| 历史| 新竹县| 天镇县| 外汇| 屏东市| 任丘市| 洛南县| 兴仁县| 长沙县| 乐平市| 金山区| 邢台市| 景洪市| 团风县| 利辛县| 乌拉特前旗| 徐汇区| 长治市| 萝北县| 凉山| 田东县| 阿城市| 临沧市| 来安县| 霍城县| 安丘市| 仁布县| 长宁县| 临沂市| 略阳县| 新密市| 柳州市| 淮阳县|