1. Introduction
Ruby is a Japanese Yukihiro Matsumoto written, easy to learn object-oriented scripting language, like Perl, rich in word processing, system management and other rich features, but Ruby is simple, easy to understand and expand. Very similar to Python, but far from being famous in the country.
Ruby is a powerful object-oriented scripting language that can be used for object-oriented programming quickly and easily. Similar to Perl, and Ruby has a powerful text processing feature that makes text processing simple. In addition, you can easily use the C language to extend Ruby functionality.
If you ever "want a simple object-oriented language", or think that "Perl is a good feature, but its syntax is really unbearable," or that "the Lisp series of languages is a nice idea, but the parentheses are everywhere, and the minimum formula should be written in the usual style." Well, Ruby might make you happy.
Summed up, Ruby has the following advantages:
? Explanatory execution, quick and easy
Ruby is an interpreted language, and its programs do not need to be compiled.
? Simple and elegant syntax
The syntax is relatively simple, similar to the ALGOL system syntax.
? Fully Object-oriented
Ruby has been designed to be a purely object-oriented language from the start, so everything is an object, such as a basic data type such as integers.
? Built-in regular engine for text-processing
Ruby supports powerful string manipulation and regular expression retrieval to facilitate the processing of strings.
? Automatic garbage collection
A garbage collection (garbage collect,gc) feature that automatically reclaims objects that are no longer in use. No user is required to manage the memory.
? Cross-platform and highly portable
Ruby supports a variety of platforms that can be run on Windows, Unix, Linux, MacOS. Ruby programs are very portable, and most programs can be run without modification on a variety of platforms.
? Elegant and perfect exception handling mechanism
Ruby provides a complete set of exception handling mechanisms that allow you to gracefully handle code handling errors.
? has many advanced features
Ruby has many advanced features, such as operator overloading, mix-ins, special methods, and so on, that make it easy to perform powerful functions with these features.
It is generally believed that he has the following characteristics:
- Simple
Interpreted language (インタプリタ), so it does not have to be compiled beforehand (コンパイル).
Variables do not have a type of distinction, although they can eliminate the worry of type errors, but also cause the vulnerability of compile-time checks.
Variables do not need to be defined.
Simple syntax, more inherited from Eiffel.
No memory management is required and the system provides a GC (ガーベージコレクタ) processing mechanism.
- Pure Object-oriented language
All objects, such as integers, are designed to be treated as objects from the beginning.
Class (クラス), inheritance, Method (メソッド), etc.
(Special method?) ) Special メソッド
Module (モジュール) によるmixin
Iterations (イテレータ) and closures (クロージャ). A resource defined at the beginning of a closure is freed after closure, suitable for sockets, database connections, file handles, and so on.
- scripting language
Interpreting execution
Strong word-processing and regular expressions
Direct access to the OS is also possible with Ruby writing system programs.
- Other
Support a variety of long integers, memory allows, can do a large number of computation operations.
With exception handling ability, with Java exception handling function.
Dynamic loading (ダイナミックローディング). You can redefine yourself at run time, and classes can also inherit/de-inherit at run time.
Thread. Now that Ruby has a thread concept, Ruby2.0 seems to support local threads.
Reflection (Reflection). Ruby can see the inside of a program, such as whether a module contains a specific method, what class an object is, and so on. Similar to Java.
Scalability. C API.
Issue licenses. Based on Ruby artistic License (BSD-style) and GPL.
2. Environment installation
(1). Install 1. You can download the source code from www.ruby-lang.org to install it.
(2). Unzip the source file into the installation directory
#./configuremake
#make
#install
3. Use
(1). Use IRB. IRB is an interactive interface that runs it in the shell, first displaying a prompt, waiting for input, and after the user has entered it, it is processed and the results are 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, as in other languages
[Email protected]/root]# chmod a+x foo.rb [email protected]/root]# cat FOO.RB #!/usr/local/bin/ruby-w Puts "Hello, world!" [Email protected]/root]# Ruby FOO.RB Hello, world!. [Email protected]/root]#./foo.rb hello,world! [Email protected]/root]# |
(3). How to use Ruby
Ruby [option ...] [ -- ] [Programfile] [Argument ...] Pre>
For ruby command-line arguments, refer to the relevant documentation.
4. Simple examples
def saygoodnight (name) result = "Goodnight," + Name return result End # Time for bed ... Puts Saygoodnight ("John-boy") Puts Saygoodnight ("Mary-ellen") |
As you can see, Ruby's syntax is relatively simple, first of all, you don't have to write a semicolon for each line, and Ruby notes start with # until the end of the line.
The methods definition starts with the keyword Def, followed by the method name and method parameters, and Ruby does not need to use braces to define the program body, only the keyword end is required.
This program is also quite simple, the first line of "Goodnight," plus the parameter name, and assign 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 the method 2 times and pass the result to the puts function, which simply re-prints the arguments on the new line to it, and the result looks like this:
Goodnight, John-boy Goodnight, Mary-ellen |
In fact puts Saygoodnight ("John-boy") consists of 2 function calls, a put system function, and a saygoodnight function. But why does the puts call not use parentheses? In fact, the following calls are all equivalent:
Puts (saygoodnight "John-boy") Puts (Saygoodnight ("John-boy")) Puts Saygoodnight ("John-boy") Puts Saygoodnight "John-boy" |
But if you don't write parentheses, do you know who that parameter is passed to? Therefore, it is recommended to add parentheses after the method to facilitate the reading of the source program.
This method also shows a string object, there are many ways to create a string object, but the most common one is to use string literals: a set of characters wrapped in single or double quotation marks. The difference is what Ruby does when it builds the two strings. Ruby does very little work for strings that are quoted in single quotes, and the single quotation marks are the ones that are the value. If it's a double quote, you'll have to do a bit more work. First, it checks to see if it contains a backslash, the escape character, and then replaces it with the appropriate binary value, the most common being "/n", which will be replaced by a newline. Such as:
Puts "and GOODNIGHT,/NGRANDMA" |
The resulting results are as follows:
The second thing is expression interpolation. #{expression} is replaced by the value of expression, for example, the following method is the same as the example shown earlier
def saygoodnight (name) result = "Goodnight, #{name}" return result End |
Of course we can also 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 a custom naming method to name the variable, the first letter of the variable name marks its type, whether it is a local variable or a method parameter, the method name should start with a lowercase letter or an underscore, the global variable should start with $, the instance variable begins with an @, the class variable begins with @@ 开头 the name of the module, Constants should start with uppercase letters.
A name can be any combination of letters, numbers, underscores, but there is no direct following a number after @.
In short, as a language, not one or two examples, several articles can be said clearly, in the online I love Ruby 10 reasons, such as the world football Top ten similar essays, always see a reason, is fun with Ruby. Perhaps, the more interesting is still in the back.
What the Ruby language is.