Ruby tutorial annotations, variable declarations, and array operations _ruby topics

Source: Internet
Author: User
Tags array definition lowercase ruby on rails

Two days ago in the "blog Park" read an article "PHP Basic Course", introduced PHP, feel good. D-Kuala is learning Ruby, just as well as writing a "Ruby starter Tutorial." To illustrate, this article is suitable for programmers who have no access to Ruby. Moreover, this article focuses on the introduction, the veteran can fly directly. (If you can help check if there is a description, or even a wrong place, also welcome to step on two feet.) D, I'm so grateful. )

Ruby Environment Build

Under Windows, the simpler way to build a ruby environment is to download a suitable version of "Rubyinstaller" (d), which is the latest version, and can be installed directly.

In addition, spit two lines, someone on the internet said that Windows is not suitable for ruby development, just start D, not believe that, yesterday because of the build ruby on Rails environment, MySQL database driver is not successful installation! It's so frustrating. So, if you really want to play Ruby, use the Linux system.

Ruby's Notes

Like other common programming languages, Ruby is also divided into "single-line annotation" and "multiline comment". The difference is that Ruby uses a different annotation symbol than Java, C + +, and other programming languages.

In addition, Ruby preference uses single-line annotations:

Copy Code code as follows:

# This is a comment line
# This is a comment line

In Ruby, multiline annotations are rare, and D is the syntax for Ruby to know more than one line of comments after more than 10 days of exposure, before wondering how to write multiple lines of syntax.

Copy Code code as follows:

=begin
This IA comment.
This is two comment
=end

In the above code, the two highlighted lines of code are the beginning and end of the comment, and the beginning of both lines must remain with the above code, otherwise the error is not correct. After the test of D-melon, you can add a comment after the beginning.

Ruby's variables

The naming of Ruby variables has its own characteristics: the first letter of a variable is used to indicate the purpose of a variable. The details are as follows:

Local variables, method parameters and method names usually begin with lowercase letters or underscores;
Global variables begin with $;
Instance variables begin with @;
Class variables begin with @@ 开头
Class names, module names, and constants begin with uppercase letters.

The naming rule for a ruby variable is that the variable name starts with a lowercase letter or an underscore (d), which is tested in IRB, can also use the $ beginning, but the error begins with the _$, and there is no problem with the $_, followed by uppercase and lowercase letters, numbers, and underscores. Variable names are case-sensitive, and the character length of the variable name is not limited. In addition, it should be noted that when naming variables, try to adhere to the above according to the variable first letter classification of the contract. D Guadalcanal thinks: The name of the variable can reflect the code literacy of a program ape.

Copy Code code as follows:

MyStr = "Are you ready?" # Local Variables

Class Person #类名

@ @year = 2013 # class variable

Def initialize (name) # Construction function name
@name = name # sample variable
End

Def Name # method name
@name
End

def name= (name)
@name = Name
End

End

Puts MyStr

Array

Less nonsense, directly on the code.

Copy Code code as follows:

# # 1, array definition
ARR1 = []
ARR2 = Array.new
ARR3 = [' 1 ', ' 2 ', ' 3 ']

# # 2, Output
Puts "output:\n"
Print ARR3, "\ n" # output: ["1", "2", "3"]
Puts Arr3[0] # output: 1
Print Arr3.join (",") # Output: 1,2,3

# # 3, accessing array elements
Puts "\narray element\n"
Puts "\ n", Arr3[0]
Puts "\ n", arr3[-1] #这个比较有意思, meaning the last data
Puts "\", Arr3.first
Puts "\", Arr3.last
Puts "\", Arr3.max
Puts "\", Arr3.min
Puts "\", Arr3.length
Puts "\", Arr3.size

# # 4, array operation
Puts "\narray method\n"
Puts "\ n", arr3[4] = ' 5 ' #此时arr3 [3] default tail nil
Puts "\ n", Arr3.delete_at (3) #删除指定下标的元素
Puts "\ n", arr3[2] = ["3a", "3b", "3c"] #可以这样来做
Puts "\ n", Arr3.delete (["3a", "3b", "3c"]) #可以删除上面添加的
Puts "\ n", Arr3.push (' 6 ') #插入元素
Puts "\ n", Arr3.insert (3, "B") #在指定位置插入新元素
Puts "\ n", arr3<< "F" <<2 #从左至右一次加入元素
Puts "\", Arr3.pop #删除尾元素
Puts "\", Arr3.shift #删除首元素
Puts "\", Arr3.clear #清空数组

# # 5, array operation
Puts "\narray operator\n"
AAA = ["AA", 4,5, "BB"]
BBB = [4,1,3,2,5]
Print AAA + BBB, "\ n" # output: ["AA", 4, 5, "BB", 4, 1, 3, 2, 5]
Print aaa*2, "\ n" # output: ["AA", 4, 5, "BB", "AA", 4, 5, "BB"]
Print bbb-aaa, "\ n" # output: [1, 3, 2]
Print AAA | BBB, "\ n" # output: ["AA", 4, 5, "BB", 1, 3, 2]
Print AAA & BBB, "\ n" # output: [4, 5]
Print Bbb.sort, "\ n" # output: [1, 2, 3, 4, 5] Sort
Print Aaa.reverse, "\ n" # output: ["BB", 5, 4, "AA"] Inverted
Print Bbb.sort.reverse, "\ n" # output: [5, 4, 3, 2, 1] Descending

Thanks to Ruby's iterator and its power, in Ruby programming, using arrays of these functions plus iterators can write very "cool" code!

This section, D Guatemala, describes comments, variable declarations, and array operations in Ruby. In the next section, D Guadalcanal will introduce strings, judgment statements, and looping statements in Ruby. Please continue to pay attention to!

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.