Original: http://hypermetrics.com/ruby.html
Translation: liubin http://www.ruby-cn.org/
All rights belong to the original author, please keep it.
2004/12/3
I don't intend to waste time to talk about Ruby's history. If you have not heard it, you can go to its homepage to see www.ruby-lang.org, or go to its newsgroup comp.lang.Ruby. If you know Ruby, I will tell why I love it. (You may also go to my Ruby home page or personal home page)
It is object-oriented. What does this mean? If you ask 10 programmers, you may get 12 results, you have your opinion, I will not try to change your opinion. However, there is a little, Ruby provides packages for data and methods, allowing the inheritance of the class, and the polymorphism of the object. Unlike other languages (C , Perl, etc.), Ruby starts from the design of an object-oriented language. It is a pure object-oriented language. Is it too much? Not this, the reason why it is said because everything in Ruby is an object, including the original data type (Primitive Data Types), such as a string, integer, is an object, without providing a packaging class like Java ( Wrapper classes). Alternatively, even constants, it is also treated as an object, so the recipient of one method can be a digital constant. It is a dynamic language. This is a major conceptual difference for people who are only familiar with C , Java. Dynamically means that methods and variables can be added and redefined at runtime. It reduces the conditional compilation (#IFDEF) like C language, and is easy to achieve reflection API (REFLECTION API). Dynamicity makes the program self-perceived, such as runtime type information, detecting missing methods, to detect hooks of increased methods. Ruby and Lisp and SmallTalk have some relationships in these aspects. It is an explanation of the language executed. This is a negative issue, it is worthy of explanation, perhaps this feature will arise from the advantages due to performance. For this, I have some insights: 1. First: Rapid development cycle is a huge benefit, which is to be executed in the explanation of Ruby. 2. How much is it called? It is said that it is a slow benchmark before it is slow. 3. Maybe someone wants to criticize me, but I still have to say: The processor has become faster every year. 4. If you are really in your speed, you can use C to develop a part of your code. 5. Finally, in a sense, this is a problem that is still arguing. No language is born is interpreted. There is no legal basis in the world to develop a Ruby compiler. It understands the regular expression. Many years ago, the regular expression is just used in UNIX, such as GREP or SED, or in VI, some findings - replacement, etc. The emergence of Perl solves these problems, and now Ruby can also do this. More and more people recognize the incredible ability of this string and text handling technology. If you expressed your doubt, then go to see Jeffrey Friedl's book Mastering Regular Expressions, then, you should not have What doubts. It is a platform. Ruby can run in Linux, UNIX, Windows, BEOS, and even MS-DOS. If I have not remembered, there is even an Amiga version it is derived. Is this a good thing? It is useful to throw the knowledge on the book. Newton once said "I am far from others, it is because I stand on the shoulder of the giant." Ruby also stands on the shoulders of the giant, which borrowed the advantages of SmallTalk, CLU, Lisp, C, C , Perl, Kornshell, etc. In my opinion, it includes: 1. Do not repeat the wheel.
2. Don't patch anything that is not damaged. 3. The last one is also particularly special, it can balance your existing knowledge. You know UNIX's files and pipes, it doesn't matter, you can continue with Ruby, you have learned a printf indicator in Ruby, don't worry, you can also use Printf in Ruby. You know the regular expression of Perl, then you have learned the regular expression in ruby. It is innovative. Do you think this and seventh contradiction? Perhaps some contradictions, each coin has two sides. Some Ruby features innovative things, such as very useful MIX-I, perhaps this feature will be drawn in later language. (Note: A reader pointed out that LISP has Mix-in 1979, this is my negligence; I should find a better example and be confident.) It is a very high-level language. (VERY HIGH-Level Language: Vhll) This is a topic that is easy to argue, because this term has not been widely used. And it means that there is still room for OOP. I said that Ruby can support complex structures and the negative operations of these structures, and the required instructions are very small, which is consistent with the Principle of Least Effort. It has a smart garbage collector. The routines like Malloc and Free are already the nightmare yesterday, you don't need to recover the memory, or even call the garbage collector. It is a scripting language. Don't think it's not strong because it is not enough. It is not a toy. It is a fully mature language that uses it to easily complete traditional script operations, such as running external programs, checking system resources, using pipeline, capture output, and more. It is common. It can also do what Kornshell does it, and the C language does it can be done very well. You can use it to write only 10 rows of programs only once, or package some legacy programs, you want to write a web server, or a CGI, you can write it with Ruby. It is multi-threaded. Y You can write multithreaded programs with some simple APIs, or even on MS-DOS. It is Open Source. Do you want to see its source code? Yes, you can also submit a patch, participate in a wide range of communities, including its creators. It is straightforward. Ruby's learning curve is relatively low, and if you have turned over a hurdle, you start "guess" how to work, and your guess is correct. Ruby adheres to the principle of lest astonishment. It has an abnormal mechanism. Like Java and C , there is an abnormal mechanism in Ruby, which means that you don't have to make the code messy, few nested IF statements, very few logic, better mistakes. . It has an advanced array: Array. Ruby's inner groups are dynamic, you don't have to declare its size like Pascal, and you don't have to allocate memory as C, C . They are objects, so you don't have to care about their length, but you can't "go to the end of the end". This class provides various methods that allow you to access array content according to the index, depending on the elements, or in reverse processing arrays. You can also use several as a set, queue, stack, etc.
If you want to look up, you can use the hash structure. It can be expanded. You can use C or Ruby to write external libraries, similarly, you can also modify existing classes and objects. Encourage Document Programming. You can embed annotations or documents in the Ruby program, which can be extracted and processed with Ruby's documentation tools. (The real document programmer may think that this is a basic thing that is a must - creative use punctuation and uppercase letters. For example, a method returns a Boolean type (there is no such statement in Ruby), then this method finally ends with the question mark, if a method is to modify the recipient itself, or have a destructive, simply end, simple, intuitive . All constants, including the class name, with a capital alphabet, all object properties begin with @ symbols. This has the utility of Hungarian nomenclature, but there is no visual ugly. Reserved Words Aren't.it's Perfectly allowable to use one an identifier thing is a so-called "reserved Word" as long assebiguity. This is a breath of fresh air. (Can be used as variables as variables Don't you understand too much.) Support iterator. This allows you to deliver a block to an object such as an array, List, Tree, and then perform Block calls for each element. This technology deserves in depth. Its security. Ruby draws on the hierarchical control mechanism based on the $ SAFE variable in Perl. This is very useful for CGI programs to prevent people from attacking the web server. There is no pointer in Ruby. Like Java, unlike C , there is no concept of pointers in Ruby, so except for headaches about the stylulation syntax and debugging. Of course, this also means that the bottom of the program will be difficult, such as accessing a device's control status register; however, we can call with a C library. (Like the C language programmer Sometimes, the Ruby programmer sometimes uses a C language to complete certain tasks) it makes people focus on detail. There are many synonyms and aliases in Ruby, you may not remember that the length of the string or array is Size or longecth, it doesn't matter, you can work any one. For Range, you can use first and end or use First and Last, they also work. You want to spell indexes, the result is written in Indexes, it doesn't matter, both of these two. Very flexible grammar. Methods When calling, parentheses can be omitted, and there is a comma partition between parameters. A group definition similar to Perl style allows you to define an array of strings without all quotes and commas. Keyword Return can be born. Rich library function. Ruby provides thread, socket, limited object persistence, CGI, server-side, other library functions such as databases, and support for TK, etc. There are still many other library functions. Itself comes back to the debugger. In the perfect world, we don't need debugger, but this world is not perfect. Interactive execution. You can use Ruby to be executed like Kornshell.
(This may be the most debate this page, I have to admit that ruby is really not a good shell. But I still insist that Ruby's shell is a good idea.) It is concise. Unlike PASCAL, it is required to follow the THEN, follow the do. Variables don't need to be declared, they don't need type. The return type does not have to be specified, the keyword return can be omitted, which will return the value of the last expression. On the other hand, it is not as complex as Perl or C. It is an expression-oriented. You can easily use the expression of x = if a <0 THEN B ELSE C. Syntax Sugar. (Interpretation of Mary Poppins: A spoonful of syntax sugar energy is accepted). If you want to iterate the array x, you can use for a in x. You can also use A = B instead of A = A B, this is. Many operators are actually a method in Ruby. The names of these methods are more intuitive, short, with convenient grammar. It supports operator overload. If I have not remembered, I have this feature as early as a long time, but it has become popular until C . Although it may be in a mess, this is still a very good advantage. In addition, Ruby automatically defines the assignment version of the operator, for example, if you redefine , then you get a = operator at the same time. Supports an unlimited accuracy. Some people will care about Short, Int, long, just use Bignum, you can easily implement 365 steps. Power operator. For a long time, we used it in Basic and Fortran, however we realized that this operator was more powerful after we learned Pascal and C. (We are told that you don't know how it works - it uses logarithm, iterates, how is it?), But do we really do? If so, we can rewrite this method, otherwise, Ruby has a very good ** operator can be used. Powerful string processing. If you want to find, judgment, format, Trim, Delimit, Interpose, Tokenize, you can choose which one you want. Rules rarely cause an exception. Ruby's grammatical and semantics are strict than other languages, each language has a unique side, and each rule has an abnormality, but the exception caused by Ruby rules is much less. Translator Note:
Article 21: Don't understand it, is it because now the version is too new, the old version may have this character?
The 34th Agendon is as follows: