Author: http://www.ruby-cn.org/reprinted please keep
1. Introduction
Ruby is written by yukihiro Matsumoto in Japan. It is a simple and easy-to-learn object-oriented scripting language. Like Perl, Ruby has rich functions such as text processing and system management, but Ruby must be simple, easy to understand and expand. It is similar to Python, but it is far from famous in China.
Generally, it has the following features:
- Simple
There is no need to compile it in advance ).
Variables are not distinguished by types. Although you can save the worry about type errors, they may also cause the vulnerability of checking during compilation.
Variables do not need to be defined.
Simple syntax is inherited from Eiffel.
Memory Management is not required. The system provides a GC (GC) processing mechanism.
- Pure object-oriented language
Everything is an object. types such as integers are designed as objects for processing from the beginning.
Class, inheritance, method, etc.
(Special method ?) Te メッ
Module (mixed) mixed Mixin
Iterations and closures ). A resource defined at the beginning of the closure will be released after the closure is completed, suitable for socket, database connection, file handle, etc.
- Script Language
Explain execution
Strong text processing capabilities and Regular Expressions
It is also possible to directly access the OS and write system programs in ruby.
- Others
Multiple long integers are supported. The memory allows large numbers of computing operations.
It has the exception handling capability and Java exception handling function.
Dynamic Loading ). You can redefine yourself at runtime, or inherit or uninherit classes at runtime.
Thread. Now Ruby already has the thread concept. ruby2.0 seems to support local threads.
Reflection (reflection ). Ruby can see the internal program, such as whether a module contains a specific method and the class of an object. Similar to Java.
Scalability. C API.
Issue a license. Based on Ruby artistic license (BSD style) and GPL.
2. Environment Installation
(1). installation 1. You can download the source code from the www.ruby-lang.org for installation.
(2) decompress the source file and enter the installation directory.
#./Configuremake
# Make
# Install
3. Use
(1). Use IRB. IRB is an interactive interface. Run it in shell. First, a prompt is displayed, waiting for input. After the user inputs it, It is processing and the result is displayed to the user.
# IRB IRB (main): 001: 0> $ STR = "Hello world! /N" => "Hello world! /N" IRB (main): 002: 0> Print $ Str Hello world! => Nil IRB (main): 003: 0> |
(2). Run from program files like other languages
[Root @ tserver/root] # chmod A + x Foo. Rb [Root @ tserver/root] # Cat Foo. Rb #! /Usr/local/bin/ruby-W Puts "Hello, world! " [Root @ tserver/root] # Ruby Foo. Rb Hello, world! [Root @ tserver/root] #./Foo. Rb Hello, world! [Root @ tserver/root] # |
(3) Ruby usage
Ruby [option...] [--] [programfile] [argument...] PRE>
For more information about Ruby command line parameters, see related documents.
4. Simple Example
Def saygoodnight (name) Result = "Goodnight," + name Return result End
# Time for bed... Puts saygoodnight ("John-Boy ") Puts saygoodnight ("Mary-Ellen ") |
As you can see, the ruby syntax is still relatively simple. First, you do not have to write a semicolon in each line. The ruby comment starts with # until the end of the line.
The definition of Methods starts with the keyword def, followed by the method name and method parameter. Ruby does not need braces to define the program subject, but only needs the keyword end.
This program is also quite simple. The first line adds ''goodnight, ''with the parameter name and assigns it to the local variable result. The second line returns the result to the caller. Note that we do not need to define the variable result.
Finally, we call this method twice and pass the result to the puts function. This function simply prints the parameter passed to it on a new line. The final result is as follows:
Goodnight, John-Boy Goodnight, Mary-Ellen |
In fact, puts saygoodnight ("John-Boy") includes two function calls, one put system function and one saygoodnight function. But why is no brackets used for puts call? In fact, the following calls are equivalent:
Puts (saygoodnight "John-Boy ") Puts (saygoodnight ("John-Boy ")) Puts saygoodnight ("John-Boy ") Puts saygoodnight "John-Boy" |
But if no parentheses are written, do you know who the parameter is passed? We recommend that you add brackets to the method to facilitate reading the source program.
This method also shows the string object, there are many ways to create a String object, but the most common is to use string literals: A group of characters enclosed by single quotes or double quotes. The difference between these two types of strings is what Ruby needs to do when constructing these two types of strings. For a string caused by single quotes, Ruby does little work, and the single quotes are the value. If it is caused by double quotation marks, you have to do more work. First, it checks whether there is a backslash, that is, an escape character, and then replaces it with an appropriate binary value. The most common one is "/N", which will be replaced by a line break. For example:
Puts "And goodnight,/ngrandma" |
The result is as follows:
The second thing is expression interpolation. # {Expression} is replaced by the expression value. For example, the following method returns the same result as the previous example.
Def saygoodnight (name) Result = "Goodnight, # {name }" Return result End |
Of course, we can simplify this function.The result returned by a ruby function is the value of the last row.So this function can also be written as follows:
Def saygoodnight (name) "Goodnight, # {name }" End |
Ruby uses the naming method as a variable. The first letter of the variable name indicates its type, whether it is a local variable or a method parameter, the method name should start with a lowercase letter or underline, the global variable should start with $, the instance variable starts with @, the class variable starts with @, and the class name, Module name, A constant must start with an uppercase letter.
A name can be any combination of letters, numbers, and underscores, but a number cannot be followed directly after.
In short, as a language, there are not one or two examples. Several articles clearly show that I love Ruby for 10 reasons on the Internet, and so on, one reason is fun with Ruby. Maybe it's more interesting.