O''''Reilly Network For Information About''''s Book part 222 pot

7 93 0
O''''Reilly Network For Information About''''s Book part 222 pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

 • Table of Contents • Reviews • Reader Reviews • Errata Ruby in a Nutshell By Yukihiro Matsumoto Publisher : O'Reilly Pub Date : November 2001 ISBN : 0 - 59600 - 214 - 9 Pages : 218 Chapter 1. Introduction Section 1.1. Ruby's Elegance Section 1.2. Ruby in Action Chapter 2. Language Basics Section 2.1. Command - Line Options Section 2.2. Environment Variables Section 2.3. Lexical Conventions Section 2. 4. Literals Section 2.5. Variables Section 2.6. Operators Section 2.7. Methods Section 2.8. Control Structures Section 2.9. Object - Oriented Programming Section 2.10. Security Chapter 3. Built - in Library Reference Section 3.1. Predefined Variables Section 3.2. Predefined Global Constants Section 3.3. Built - in Functions Section 3.4. Built - in Library Chapter 4. Standard Library Reference Section 4.1. Standard Library Chapter 5. Ruby Tools Section 5.1. Standard Tools Section 5.2. Additional Tools Section 5.3. Ruby Application Archive Chapter 6. Ruby Updates Section 6.1. Summary of Changes Section 6.2. Changes from 1.6.5 to 1.7.1 Section 6.3. The Future of Ruby Section 6.4. Participate in Ruby Top Ruby in a Nutshell By Yukihiro Matsumoto Chapter 1. Introduction Ruby has been readily adopted by programmers in Japan and has had much documentation written for it in Japanese. As programmers outside of Japan learn about the benefits of Ruby, there is a growing need for documentation in English. The first book I wrote for O'Reilly, Ruby Pocket Reference , was in Japanese. Since then Ruby has changed significantly. To meet the needs of non-Japanese programmers, we translated, updated, and expanded Ruby Pocket Reference into Ruby in a Nutshell. Ruby is an object-oriented programming language that makes programming both enjoyable and fast. With the easy-to-use interpreter, familiar syntax, complete object-oriented functionality, and powerful class libraries, Ruby has become a language that can be applied to a broad range of fields from text processing and CGI scripts to professional, large-scale programs. While Ruby is easy to learn, there are many details that you can't be expected to remember. This book presents those details in a clean and concise format. It is a reference to keep next to your desktop or laptop, designed to make Ruby even easier to use. For those of you who are new to Ruby, there are several online tutorials available to get you started: Ruby's home page (http://www.ruby-lang.org/ ) is a good starting pointing as it offers Ruby tutorials and the Ruby Language FAQ. Top Ruby in a Nutshell By Yukihiro Matsumoto Chapter 1. Introduction 1.1 Ruby's Elegance Ruby is a genuine object-oriented scripting language designed from the ground up to support the OOP model. Most modern languages incorporate aspects of object-oriented programming. Because Ruby was designed from the beginning to support OOP, most programmers feel it is elegant, easy to use, and a pleasure to program. Everything in Ruby is an object; there's no exception. While Ruby is object-oriented, you can also use Ruby to do procedural programming. But as you do, Ruby is secretly turning your nifty procedures into methods on a globally accessible object. Throughout the development of the Ruby language, I've focused my energies on making programming faster and easier. To do so, I developed what I call the principle of least surprise. All features in Ruby, including object-oriented features, are designed to work as ordinary programmers (e.g., me) expect them to work. Here are some of those features: Interpretive programming No compilation is needed; you can edit and feed your program to the interpreter. The faster development cycle helps you enjoy the programming process. Dynamic programming Almost everything in Ruby is done at runtime. Types of variables and expressions are determined at runtime as are class and method definitions. You can even generate programs within programs and execute them. Familiar syntax If you've been programming in Java, Perl, Python, C/C++, or even Smalltalk, Ruby's syntax is easy to learn. The following simple factorial function illustrates how easily you can decipher its meaning: def factorial(n) if n == 0 return 1 else return n * factorial(n-1) end end Iterators The iterator feature for loop abstraction is built into the language, which means a block of code can be attached to a method call. The method can call back the block from within its execution. For example, Array has the each method to iterate over its contents. With this feature, you don't need to worry about the loop counter or boundary condition. ary = [1,2,3,4,5] ary.each do |i| puts 1*2 end # prints 2,3,4,8,10 for each line A block is used not only for loops. It can be used for various purposes including the select method of Array, which uses blocks to choose values that satisfy conditions from contents: ary = [1,2,3,4,5] ary = ary.select do |i| i %2 == 0 end # returns array of even numbers. Exceptions Just as you'd expect in a modern OOP language, Ruby provides language- level support for exception handling. For example, an attempt to open a file that doesn't exist raises an exception, so that your program doesn't run, assuming an unmet precondition. This feature obviously enhances the reliability of your programs. Exceptions can be caught explicitly using the rescue clause of the begin statement: begin f = open(path) rescue puts "#{path} does not exist." exit 1 end Class libraries Ruby comes with a strong set of bundled class libraries that cover a variety of domains, from basic datatypes (strings, arrays, and hashes) to networking and thread programming. The following program retrieves the current time string from the local host via a network socket connection: require "socket" print TCPSocket.open("localhost","daytime").gets In addition to bundled libraries, if you go to http://www.ruby- lang.org/en/raa.html shows a list of the many unbundled useful libraries along with applications and documentation. Since Ruby is rather young, the number of libraries available is smaller than that of Perl, for example, but new libraries are becoming available each day. Portable Ruby ports to many platforms, including Unix, DOS, Windows, OS/2, etc. Ruby programs run on many platforms without modification. Garbage collection Object-oriented programming tends to allocate many objects during execution. Ruby's garbage collector recycles unused object automatically. Built-in security check Ruby's taint model provides safety when handling untrusted data or programs. . documentation written for it in Japanese. As programmers outside of Japan learn about the benefits of Ruby, there is a growing need for documentation in English. The first book I wrote for O'Reilly,. remember. This book presents those details in a clean and concise format. It is a reference to keep next to your desktop or laptop, designed to make Ruby even easier to use. For those of you. [1,2,3,4,5] ary.each do |i| puts 1*2 end # prints 2,3,4,8,10 for each line A block is used not only for loops. It can be used for various purposes including the select method of Array, which

Ngày đăng: 07/07/2014, 08:20

Tài liệu cùng người dùng

Tài liệu liên quan