📄 00000000.htm
字号:
declare their size at compile-time as in, say, Pascal. You don't have to <BR> allocate memory for them as in C, C++, or Java. They're objects, so you <BR> don't have to keep up with their length; it's virtually impossible to <BR>"walk off the end" of an array as you might in C. Want to process them <BR>by index? By element? Process them backwards? Print them? There are <BR>methods for all these. Want to use an array as a set, a stack, or a <BR>queue? There are methods for these operations, too. Want to use an array <BR> as a lookup table? That's a trick question; you don't have to, since we <BR> have hashes for that. <BR> <BR>It's extensible. You can write external libraries in Ruby or in C. In <BR>addition, you can modify the existing classes and objects at will, on <BR>the fly. <BR> <BR>It encourages literate programming. You can embed comments in your <BR>code which the Ruby documentation tool can extract and manipulate. (Real <BR> fans of literate programming may think this is pretty rudimentary.) <BR> <BR>It uses punctuation and capitalization creatively. A method returning <BR>a Boolean result (though Ruby doesn't call it that) is typically ended <BR>with a question mark, and the more destructive, data-modifying methods <BR>are named with an exclamation point. Simple, informative, and intuitive. <BR> All constants, including class names, start with capital letters. All <BR>object attributes start with an @ sign. This has the pragmatism of the <BR>old "Hungarian notation" without the eye-jarring ugliness. <BR> <BR>Reserved words aren't. It's perfectly allowable to use an identifier <BR>that is a so-called "reserved word" as long as the parser doesn't <BR>perceive an amibiguity. This is a breath of fresh air. <BR> <BR>It allows iterators. Among other things, this makes it possible to <BR>pass blocks of code to your objects in such a way that the block is <BR>called for each item in the array, list, tree, or whatever. This is a <BR>powerful technique that is worth exploring at great length. <BR> <BR>It has safety and security features. Ruby borrows Perl's concept of <BR>tainting and allows different levels of control (levels of paranoia?) by <BR> means of the $SAFE variable. This is especially good for CGI programs <BR>that people will try to subvert in order to crack the web server. <BR> <BR>It has no pointers. Like Java, and with a grudging nod to C++, Ruby does <BR> not have the concept of a pointer; there is no indirection, no <BR>pointer arithmetic, and none of the headaches that go with the syntax <BR>and the debugging of pointers. Of course, this means that real <BR>nuts-and-bolts system programming is more difficult, such as accessing a <BR> control-status register for a device; but that can always be done in <BR>a C library. (Just as C programmers drop into assembly when necessary, <BR>Ruby programmers drop into C when they have to!) <BR> <BR>It pays attention to detail. Synonyms and aliases abound. You can't <BR>remember whether to say size or length for a string or an array? <BR>Either one works. For ranges, is it begin and end, or first and last? <BR>Take your pick. You spell it indices, and your evil twin spells it <BR>indexes? They both work. <BR> <BR>It has a flexible syntax. Parentheses in method calls can usually be <BR>omitted, as can commas between parameters. Perl-style quotes allow <BR>arrays of strings without all the quotation marks and commas. The return <BR> keyword can be omitted. <BR> <BR>It has a rich set of libraries. There is support for threads, sockets, <BR>limited object persistence, CGI programs, server-side executables, DB <BR>files, and more. There is some support for Tk, with more on the way. <BR> <BR>It has a debugger. In a perfect world, we wouldn't need debuggers. <BR>This is not a perfect world. <BR> <BR>It can be used interactively. Conceivably it could be used as a sort <BR>of "Kornshell squared." <BR> <BR>It is concise. There are no superfluous keywords such as Pascal's begin, <BR> then after if, do after while. Variables need not be declared, as <BR>they do not have types. Return types need not be specified for methods. <BR> The return keyword is not needed; a method will return the last <BR>evaluated expression. On the other hand... it is not so cryptic as C <BR>or Perl. <BR> <BR>It is expression-oriented. You can easily say things like x = if a<0 <BR>then b else c. <BR> <BR>It is laced with syntax sugar. (To paraphrase Mary Poppins: A spoonful <BR>of syntax sugar helps the semantic medicine go down.) If you want to <BR>iterate over an array x by saying for a in x, you can. If you want to <BR>say a += b instead of a = a + b, you can. Most operators are really just <BR> methods with short, intuitive names and a more convenient syntax. <BR> <BR>It has operator overloading. If I am not mistaken, this originated <BR>long ago in SNOBOL, but was popularized more recently by C++. It can <BR>be overdone or misused, but it can be nice to have. Additionally, Ruby <BR>defines the assignment version of an operator automagically; if you <BR>define +, you get += as a bonus. <BR> <BR>It has infinite-precision integer arithmetic. Who cares about short, <BR>int, long? Just use a Bignum. Admit it, you always wanted to find the <BR>factorial of 365. Now you can. <BR> <BR>It has an exponentiation operator. In the old days, we used this in <BR>BASIC and FORTRAN. But then we learned Pascal and C, and learned how <BR>evil this operator was. (We were told we didn't even know how the <BR>evaluation was done -- did it use logarithms? Iteration? How efficient <BR>was it?) But then, do we really care? If so, we can rewrite it <BR>ourselves. If not, Ruby has the good old ** operator you loved as a <BR>child. Enjoy it. <BR> <BR>It has powerful string handling. If you want to search, substitute, <BR>justify, format, trim, delimit, interpose, or tokenize, you can probably <BR> use one of the built-in methods. If not, you can build on them to <BR>produce what you need. <BR> <BR>It has few exceptions to its rules. The syntax and semantics of Ruby are <BR> more self-consistent than most languages. Every language has oddities, <BR> and every rule has exceptions; but Ruby has fewer than you might <BR>expect. <BR> <BR> <BR> <BR>-- <BR> 桃花坞里桃花庵,桃花庵下桃花仙;桃花仙人种桃树,又摘桃花卖酒钱。 <BR> 酒醒只在花前坐,酒醉换来花下眠;半醒半醉日复日,花落花开年复年。 <BR> 但愿老死花酒间,不愿鞠躬车马前;车尘马足富者趣,酒盏花枝贫者缘。 <BR> 若将富贵比贫贱,一在平地一在天;若将贫贱比车马,他得驱驰我得闲。 <BR> 别人笑我忒疯癫,我笑他人看不穿;不见五陵豪杰墓,无花无酒锄做田。 <BR> <BR> <BR>※ 来源:·BBS 水木清华站 smth.org·[FROM: 202.204.7.234] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -