What is the ruby language?
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.
Ruby is a powerful object-oriented scripting language that can be used for convenient and convenient object-oriented programming. Similar to Perl, Ruby provides powerful text processing capabilities to simplify text processing. In addition, you can easily use the C language to expand Ruby functions.
If you "Want a simple object-oriented language", or think that "Although Perl functions are easy to use, its syntax is really unbearable ", or you may think that "the idea of the LISP series of languages is good, but the brackets are everywhere. At least the formula should be written in the usual style ". Ruby may make you satisfied.
Ruby has the following advantages:
Interpreted execution, convenient and fast
Ruby is an interpreted language that can be executed without compilation.
Simple and elegant syntax
The syntax is relatively simple, similar to the Algol syntax.
Fully object-oriented
Ruby was designed as a pure object-oriented language from the very beginning, so everything is an object, such as an integer and other basic data types.
Built-in Regular Expression Engine for text processing
Ruby supports powerful string operations and regular expression retrieval functions to conveniently process strings.
Automatic garbage collection
With the Garbage Collect (GC) function, you can automatically recycle objects that are no longer in use. You do not need to manage the memory.
Cross-platform and highly portable
Ruby supports multiple platforms and runs on Windows, Unix, Linux, and MacOS. Ruby programs are very portable, and most programs can be run on various platforms without modification.
Elegant and complete Exception Handling Mechanism
Ruby provides a complete set of exception handling mechanisms to handle code errors elegantly.
Has many advanced features
Ruby has many advanced features, such as operator overloading, Mix-ins, and special methods. These features allow you to easily complete various powerful functions.
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.