In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what are the variables of Ruby", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what are the variables of Ruby" this article.
Ruby supports five types of variables, namely, global variables, instance variables, class variables, local variables, constants, and pseudo variables.
Ruby global variable
The global variable starts with $. The value of the uninitialized global variable is nil, and a warning is generated after using the-w option. Assigning values to global variables changes the global state, so it is not recommended to use global variables. The following example shows the use of global variables. Example
#! / usr/bin/ruby#-*-coding: UTF-8-*-$global_variable = 10class Class1 def print_global puts "Global variable is output in Class1 as # $global_variable" endendclass Class2 def print_global puts "Global variable is output as # $global_variable" endendclass1obj = Class1.newclass1obj.print_globalclass2obj = Class2.newclass2obj.print_global in Class2
Here, $global_variable is a global variable. This produces the following result: note: in Ruby, you can access the value of any variable or constant by placing the # character before it.
Global variables are output as 10 in Class1 and global variables are output as 10Ruby instance variables in Class2
The instance variable begins with @. The value of the uninitialized instance variable is nil, and a warning is generated after using the-w option. The following example shows the use of instance variables. Example
#! / usr/bin/rubyclass 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" endend# 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 ()
In this case, @ cust_id, @ cust_name, and @ cust_addr are instance variables. This will produce the following results:
Customer id 1Customer name JohnCustomer address Wisdom Apartments, LudhiyaCustomer id 2Customer name PoulCustomer address New Empire road, KhandalaRuby class variables
Class variables begin with @ @ and must be initialized before they can be used in the method definition. Referencing an uninitialized class variable results in an error. A class variable can be shared among subclasses or submodules of the class or module in which it is defined. Overloading class variables generates a warning after using the-w option. The following example shows the use of class variables. Example
#! / usr/bin/rubyclass 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" endend# create object cust1=Customer.new ("1", "John" "Wisdom Apartments, Ludhiya") cust2=Customer.new ("2", "Poul", "New Empire road, Khandala") # call method cust1.total_no_of_customers () cust2.total_no_of_customers ()
Here, @ @ no_of_customers is a class variable. This will produce the following results:
Total number of customers: 1Total number of customers: 2Ruby local variable
Local variables begin with a lowercase letter or an underscore _. The scope of the local variable ranges from class, module, def, or do to the corresponding end or from the left brace to the right brace {}. When an uninitialized local variable is called, it is interpreted as calling a method without parameters. Assigning values to uninitialized local variables can also be treated as variable declarations. The variable will exist until the end of the current domain. The life cycle of a local variable is determined when the Ruby parser is used. In the above example, the local variables are id, name, and addr.
Ruby constant
Constants begin with uppercase letters. Constants defined within a class or module can be accessed from within the class or module, and constants defined outside the class or module can be accessed globally. Constants cannot be defined in a method. Referencing an uninitialized constant results in an error. A warning is generated for a constant assignment that has been initialized. Example
#! / usr/bin/ruby#-*-coding: UTF-8-*-class Example VAR1 = 100VAR2 = 200def show puts "the first constant has a value of # {VAR1}" puts "the second constant has a value of # {VAR2}" endend# create object object=Example.new () object.show
Here, VAR1 and VAR2 are constants. This will produce the following results:
The value of the first constant is 100. the value of the second constant is a 200Ruby pseudo variable.
They are special variables that look like local variables, but behave like constants. You cannot assign any values to these variables.
1.self: the sink object for the current method. 2.true: the value that represents true. 3.false: the value that represents false. 4.nil: the value that represents undefined. 5.FILE: name of the current source file. 6.LINE: number of the current line in the source file.
These are all the contents of the article "what are the variables of Ruby". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.