Latest web development tutorials

Ruby variables

Variable is holding any data can be used by any program stored.

Ruby supports five types of variables.

  • Generally lower case letters, beginning with an underscore: Variable (Variable).
  • $ At the beginning: a global variable (Global variable).
  • @ The beginning: the instance variables (Instance variable).
  • @@ Beginning: Class variable (Class variable) Class variables are shared across the inheritance chain
  • Start with a capital letter: Constant (Constant).

You have in previous chapters about understanding these variables, this chapter will provide you a detailed explanation of these five types of variables.

Ruby global variable

Global variables start with $. Uninitialized global variable isnil,in the -w option, a warning is generated.

To the global variable assignment will change global state, so we do not recommend the use of global variables.

The following example shows the use of global variables.

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

$global_variable = 10
class Class1
  def print_global
      puts "全局变量在 Class1 中输出为 #$global_variable"
  end
end
class Class2
  def print_global
      puts "全局变量在 Class2 中输出为 #$global_variable"
  end
end

class1obj = Class1.new
class1obj.print_global
class2obj = Class2.new
class2obj.print_global

Here, $ global_variable is a global variable. This produces the following results:

Note: In Ruby, you can place the # character in front of a variable or constant, to access any variable or constant value.

全局变量在 Class1 中输出为 10
全局变量在 Class2 中输出为 10

Ruby instance variables

@ Instance variable to begin with. Uninitialized instance variable isnil,in the -w option, a warning is generated.

The following example shows the use of instance variables.

#! / Usr / bin / ruby

class Customer
   def initialize (id, name, addr)
      @ Cust_id = id
      @ Cust_name = name
      @ Cust_addr = addr
   end
   def display_details ()
      puts "Customer id # @ cust_id"
      puts "Customer name # @ cust_name"
      puts "Customer address # @ cust_addr"
    end
end

# Create Object cust1 = Customer.new ( "1", "John", "Wisdom Apartments, Ludhiya")
cust2 = Customer.new ( "2", "Poul", "New Empire road, Khandala")

# Call method cust1.display_details ()
cust2.display_details ()

Here, @ cust_id, @ cust_name and @cust_addr is an instance variable. This produces the following results:

Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala

Ruby class variables

In the beginning of @@ class variables and must be initialized before you can use later in the method definition.

Reference an uninitialized variable class will generate an error. Class variables defined in the class or subclass of modules or sub-modules can be shared.

After using the -w option, overloading class variables will produce a warning.

The following example shows the use of class variables.

#!/usr/bin/ruby

class Customer
   @@no_of_customers=0
   def initialize(id, name, addr)
      @cust_id=id
      @cust_name=name
      @cust_addr=addr
   end
   def display_details()
      puts "Customer id #@cust_id"
      puts "Customer name #@cust_name"
      puts "Customer address #@cust_addr"
    end
    def total_no_of_customers()
       @@no_of_customers += 1
       puts "Total number of customers: #@@no_of_customers"
    end
end

# 创建对象
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

# 调用方法
cust1.total_no_of_customers()
cust2.total_no_of_customers()

Here, @@ no_of_customers is a class variable. This produces the following results:

Total number of customers: 1
Total number of customers: 2

Ruby local variables

Local variables begin with a lowercase letter or an underscore _. The scope of local variables from class, module, def or do the corresponding end of the brace to the left or right curly braces {}.

When you call an uninitialized local variable, it is interpreted as calling a method with no parameters.

Uninitialized local variable assignment can also be used as a variable declaration. Variable will exist until the end of the current domain so far. Lifecycle local variables determined at Ruby parser.

In the example above, the local variable is the id, name and addr.

Ruby constants

Constant with an uppercase letter. Define constants within the class or module can be accessed from within the class or module, defined outside a class or module constants can be accessed globally.

Constants can not be defined within the method. Reference an uninitialized constant error occurs. Already initialized constant values ​​will generate a warning.

#! / Usr / bin / ruby
# - * - Coding: UTF-8 - * -

class Example
   VAR1 = 100
   VAR2 = 200
   def show
       puts "The first constant is # {VAR1}"
       puts "The second constant is # {VAR2}"
   end
end

# Create Object object = Example.new ()
object.show

Here, VAR1 and VAR2 are constants. This produces the following results:

The first constant value of 100
The second constant is 200

Ruby pseudo-variable

They are special variables, with the appearance of local variables, but acts like a constant. You can not assign any value to these variables.

  • self: receiver objects of the current method.
  • true: the representative of the true value.
  • false: The value represents false.
  • nil: Representative undefined value.
  • __FILE__: The current name of the source file.
  • __LINE__: Current source file line number.