A brief analysis of the variables and assignment of Ruby

Source: Internet
Author: User
Tags lowercase uppercase letter

Variables and Assignments

At this point, have you noticed that there is something missing in all of the previous sample code? Do you have to enter constants, instance variables, or class variables? That's absolutely not it! This is part of Ruby's true object-oriented nature. To do this, let's first look at the previous generic variables in Ruby. So far, you've created a lot of rectangle instances, but you haven't kept them for long. For example, you want to assign a variable to a rectangle instance you create:

Myrectangle=rectangle.new (4,5)

This is a perfectly valid code in Ruby, and it doesn't require another line of code to type or declare myrectangle as something that references rectangle. After this line of code is executed, the variable myrectangle references an instance of the rectangle (height and width values are 4,5 respectively). However, this is only an object reference that can be changed at any time, regardless of the type of object (everything in Ruby is an object). Therefore, you can easily assign a myrectangle to a string in the following command prompt line:

IRB (main):049:0< myrectangle=rectangle.new (4,5)
=> #<rectangle:0x587c758 @width =5, @height =4>
IRB (main):050:0< myrectangle= "Jim ' s Rectangle"
=> "Jim ' s Rectangle"

You can experiment with many other programming languages (and even object-oriented languages like Java) and observe the compilation errors generated from your IDE.

variables, instance variables, class variables, and even "constants" are actually just object references. They refer to objects, but they are not the objects themselves. As a result, they can be changed dynamically or even refer to another object of different types.

Because of this flexibility, you have to make some conventions in Ruby to help everyone know that a variable is being used by the code. In fact, you've seen one of them (@ symbol, it means that this is an instance variable). Other variables, methods, and class naming conventions are listed in table 1 below.

Local variables and method parameters begin with a lowercase letter.

The method name begins with a lowercase letter.

The global variable begins with a $.

The instance variable begins with a @.

The class variable starts with a two @.

Constants begin with an uppercase letter (they are often specified in all capitals).

Class and module names begin with an uppercase letter.

local variable global variable instance variable class variable constant class name method name
$Var @var var myclassmy method
n Ame $debug @lastName @ @interest rectangle area

Table 1. This table contains examples of the Ruby coding conventions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.