Annotations, variable declarations, Array Operations, and Ruby variables in the ruby tutorial
I read an article "PHP basic tutorial" on "blog Park" two days ago and introduced PHP. D guago also wrote a "Ruby getting started tutorial" when learning Ruby ". It should be noted that this article is suitable for beginners who have programming basics but have never been familiar with Ruby. In addition, this article focuses on getting started, and laruence can fly directly. (If you can check whether the descriptions are inappropriate or even incorrect, you are welcome to step on your feet. D. You are very grateful .)
Ruby Environment Construction
In Windows, to build a Ruby environment, you can download a suitable version on "RubyInstaller" (D guago uses the latest version) and install it directly.
In addition, some people on the Internet say that Windows is not suitable for Ruby development. At the beginning, D guago did not believe it. Yesterday, because of the Ruby on Rails environment, failed to install the Mysql database driver! It's too tough. Therefore, if you really want to play Ruby, use the Linux system.
Ruby annotations
Like other common programming languages, Ruby is also divided into "single line comment" and "multi-line comment ". The difference is that Ruby uses different annotation symbols than Java, C/C ++, and other programming languages.
In addition, Ruby prefers to use a single line comment:
Copy codeThe Code is as follows:
# This is a comment line
# This is a comment line
In Ruby, multi-line comments are rare. D guago learned the syntax of multi-line comments after 10 days of contact with Ruby. He has been wondering How to Write multi-line comments.
Copy codeThe Code is as follows:
= Begin
This ia comment.
This is two comment
= End
In the above Code, the highlighted lines of code are the beginning and end of the annotation. The beginning of the two lines must be consistent with the above Code; otherwise, an error is reported. After a test by D guago, you can add comments at the beginning.
Ruby Variables
Ruby variable naming has its own characteristics: the first letter of the variable is used to indicate the purpose of the variable. The details are as follows:
Local variables, method parameters, and method names are generally started with lowercase letters or underscores;
The global variable starts with $;
Instance variables start;
Class variables start;
Class Name, Module name, and constant start with an uppercase letter.
The naming rules for Ruby variables are as follows: the variable name starts with a lowercase letter or underscore (D guago can also start with $ in IRB, but an error will be reported if it starts with _ $; it can start with $ _), followed by uppercase/lowercase English letters, numbers, and underscores. Variable names are case-sensitive and there is no limit on the Character length of variable names. In addition, it should be noted that when naming a variable, we should try to abide by the above conventions based on the first letter of the Variable Classification. D guago believes that the naming of variables can reflect the code literacy of a programmer.
Copy codeThe Code is as follows:
Mystr = "Are you ready? "# Local variables
Class Person # class Name
@ Year = 2013 # class variable
Def initialize (name) # name of the constructor
@ Name = name # sample variable
End
Def name # method name
@ Name
End
Def name = (name)
@ Name = name
End
End
Puts mystr
Array
Let alone code.
Copy codeThe Code is 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. Access array elements
Puts "\ nArray Element \ n"
Puts "\ n", arr3 [0]
Puts "\ n", arr3 [-1] # this is interesting, meaning the last data
Puts "\ n", arr3.first
Puts "\ n", arr3.last
Puts "\ n", arr3.max
Puts "\ n", arr3.min
Puts "\ n", arr3.length
Puts "\ n", arr3.size
#4. Array Operations
Puts "\ nArray method \ n"
Puts "\ n", arr3 [4] = '5' # arr3 [3] default end nil
Puts "\ n", arr3.delete _ at (3) # Delete the specified Element
Puts "\ n", arr3 [2] = ["3a", "3b", "3c"] # You can do this
Puts "\ n", arr3.delete (["3a", "3b", "3c"]) # You can delete
Puts "\ n", arr3.push ('6') # insert an element
Puts "\ n", arr3.insert (3, "B") # Insert a new element at a specified position
Puts "\ n", arr3 <"f" <2 # add elements from left to right
Puts "\ n", arr3.pop # Delete the End Element
Puts "\ n", arr3.shift # Delete the first element
Puts "\ n", arr3.clear # Clear the Array
#5. Array Operations
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] sorting
Print aaa. reverse, "\ n" # output: ["bb", 5, 4, "aa"] inverted
Print bbb. sort. reverse, "\ n" # output: [5, 4, 3, 2, 1] in descending order
Ruby's iterators are powerful. In Ruby programming, you can use these functions of the array plus the iterator to write very "dazzling" code!
This section describes the annotations, variable declarations, and Array Operations in Ruby. In the next section, Dr. D will introduce the strings, judgment statements, and loop statements in Ruby. Stay tuned!
Ruby array storage value
Why array?
You can use the gets function to obtain the keyboard input. You can go to irb for an interview:
Puts "I am # {puts }"
For example:
Names = []
Names. push (gets)
In this way, the input content becomes the first element in the array. Is that what you want?
Ruby array Problems
A = [] # a = Array. new
10. times do
Input = gets. chomp
A <input. to_ I
End
# A. each do | j |
# Print j. to_s, "\ t"
# End