Ruby entry-level authoritative classics (1)

Source: Internet
Author: User

Author: published by tianen Beijing Aerospace Publishing House.

Ruby is a language with the word "Ruby ". Rails is a technical framework. Its word is "Rails ". The meaning of "Ruby on Rails" is that it is full of Ruby rails. Rails is a B/S structure programming framework based on the ruby language. This is similar to ASP Based on VBScript and JSP (struts) based on Java.

It is okay to learn Ruby separately. You can use Ruby to write variousProgram. However, this is usually not the case. For me, the best universal language is Java, and it is very comfortable to develop Java programs using JBuilder. If you do not plan to use Ruby as a universal language, you 'd better use it for web development, that is, using the Ruby on Rails framework, which is Ruby's best application.

The person who is born and knows is also; the person who learns and knows is also the person who is sleepy and knows is also the second person who knows.

"There are people who can be trapped, there are also people who can not be trapped", trapped in technology, is not worth it.

Savvy, varies from person to person, cannot be forced, desire, varies from person to person, should not be persistent.

It is true that human resources are very important, very important, and very important. But there are a few words to remind readers: "diligence is good training, and it is only a hard job ". Tianzi is provided by Tianzhi, not by humans. However, in addition to tianzi, you still have a lot to do. Through diligence, persistence, and hard work, even if you are too talented, you will surely become a genius that surpasses ordinary people.

Ruby features: (1) Interpretation and execution, which can be run without compilation. This increases the debugging speed, but the execution efficiency is low. (2) variables have no type, and the error checking capability is weak, case sensitive. (3) variables do not need to be declared (4) memory management is not required, and objects that are no longer useful can be automatically recycled. (5) purely object-oriented, including integers and other basic data types, all of which have a unified interface for sending messages. (6) The iterator abstracts the loop so thatCodeConcise. (7) powerful string operations (8) super-long integers (9) can directly access the operating system. Therefore, you can use Ruby alone for system programming. (10) use modules for hybrid insertion, discard multiple inheritance, and use modules to share data and methods beyond the limitations of classes. (11) closure, which can visualize a process segment. (12) Dynamic Loading. You can read object files at runtime.

Chapter 1 basics of Ruby

Note that if the installation version is 1.9.x, it is recommended that 1.8.x be installed.

(1) The quotation marks in Ruby include single quotation marks and double quotation marks. single quotation marks can contain double quotation marks, and double quotation marks can also contain single quotation marks. In most cases, these two symbols are generic.

(2) escape characters: \, \ t, and \ n.

(3) A semicolon is the connector of a statement. Line Break: \ n. Note: If the output string does not end with a linefeed, The puts method adds a linefeed after the output statement. If the output string ends with a linefeed, then, the puts method does not add line breaks at the end of the statement.

(4) line feed: when the statement is too long, it will affect reading and want to be divided into multiple lines for writing. One way is to use a string connector to connect a statement, but the string connector is an operator that consumes resources. The hyphen belongs to the format control character "\".

(5) annotator: "#" is used for single-line comments; "= begin" and "= end" are used for multi-line comments.

(6) local variables: Common variables starting with lowercase letters or underscores in Ruby. If local variables are not initialized before use, an error occurs ..

(7) Basic Input: Use the gets method in Ruby to solve the most basic input problems. For example, you can use a = gets to save the characters you entered in the local variable.

(8) Basic output: puts and print.

(9) Number and string connections: Ruby supports integer and floating point types. Use "+" as the string Connection Symbol.

**************************************** **************************************** ******************

(1) Class: Ruby is more object-oriented than Java, and everything is an object. Therefore, there are no functions in Ruby, and only methods are available. All variables and constants in ruby cannot be separated from classes.

(2) simplest class: Attribute + method. In Ruby, the class name starts with an uppercase letter and the instance property starts with "@". The variable and method names should start with a lowercase letter or underline. The method for defining a class is to use the "def... end" Statement block. If you need a return value, you can use the "return" statement. Class initialization uses the "initialize" method.

(3) basic usage of classes: Ruby uses the New Method to Construct class instances, for example, M = man. New. After constructing an instance, you can use the class method.

(4) Inheritance: Ruby uses the "<" symbol to implement class inheritance, which is different from "extends" in Java and ":" in C ++. To enhance the parent class method in a subclass, use "super" as the keyword (if the parent class method to be enhanced has parameters, use the super (parameter) method to enhance it ).

(5) Single-state method: Ruby is a weak type language and only needs to be rewritten without being overloaded. Therefore, there is no way to show its polymorphism. However, Ruby has a single-state method, which means that multiple objects derived from a common class can have different methods.

(6) method Access Control: In Ruby, there are only methods and no functions. Each method exists in the class. If the method is not defined in any class, it should belong to the object class. Theoretically, this method can be used by all objects, but if it is true, it will be messy. Therefore, Ruby implements the object class as a private method, so it cannot be used by objects. Ruby provides three keywords to restrict access to methods: pubulic, which can be used to access any object (public is the default setting of all methods); protected, it can be called in the instance that defines it or the instance of the subclass; private can only be used in the object where this method is located, and cannot directly call the private method of another object, the call itself is not allowed. These keywords are inserted into the code between two methods. All methods defined from the private keyword are private, so long as there is another access control keyword in the code. The initialize method of a class is automatically private. (In addition to setting the access permission before a method, you can also set the permission after the method is defined, for example, public: free private: sayname,: sayage ).

(7) attribute read/write control: attributes in the class, including read-only attributes, write-only attributes, and read/write attributes. You can use functions similar to set and get to set the parameters.

(8) garbage collection: Ruby implements automatic memory management and garbage collection. It does not have a Finalize method similar to Java, but it has a GC class similar to. net for garbage cleanup and memory collection. The GC class has four methods: Disable, enable, start, and garbage_collect. the first two methods are to disable and start the Garbage Collector. The start method and garbage_collect have the same meaning and call the start method to implement the garbage collection function.

(9) Exception Handling: Ruby uses the begin... rescue... end structure to handle exceptions, which is similar to try... catch... finally in Java. The application methods are always monitored in a specific block. if an exception is found, it is processed. The retry statement can re-execute the content in the begin block. The raise statement in Ruby throws an exception, similar to the throw statement in Java. The exception information can be changed to a global variable $! . In Ruby, The ensure statement is used to clean up the block, similar to the finally statement in Java. Note: In begin... rescue... the end block can only use ensure or rescue (this is similar to try... catch... fiannly), but if both ensure and rescue are used, ensure must be behind rescue.

**************************************** **************************************** ******************

(1) Ruby has three types of variables, a constant, and two strictly variables. Both variables and constants have no type. Variables do not need to be declared in Ruby, and constants and variables are classified by the first letter identifier.

Example of variable type description

The local variable must contain lowercase letters or underscores (_ var ).
Global variables start with $ VaR
Class variable class definition, starting with @ VaR
Defined in the instance variable object, starting with @ VaR
Constant starts with an uppercase letter VaR

Note: Ruby's variable self always points to the currently executed object or the null NIL of the uninitialized variable. Although both are named as local variables, self is a global variable held by the interpreter, while Nil is actually a constant.

(2) constant: a constant starts with an uppercase letter and is assigned a maximum of once. However, if the value is assigned multiple times, only warnings are generated and no errors are generated.

(3) global variables: The global variables start with "$" and can be accessed at any position of the program. Before initialization, the global variable has a special value nil.

$! : Last error message; $ _: String recently read by gets; $ &: string that matches the latest Regular Expression $ N: Recently matched n subexpressions $ /: input record separator $0: Ruby script file name $: Interpreter process ID $ @: location where the error is generated $.: number of lines recently read by the interpreter $ ~ : Last match as a sub-expression group $ =: case-sensitive flag $ \: Output record style Fu $ *: Command Line Parameter $? : The exit status of the last executed sub-process.

(4) Unless statement: used to select conditions. It is a negative version selected by "if. Multiple choice statement: Case (combined with when), equivalent to the switch statement in C ++.

(5) While loop: A single while loop statement while condition; until loop until condition code end; for... in loop for variable in set object end.

Loop Control: the break jumps out of the current loop. Next ignores the remaining parts of this loop and starts the next loop, which is equivalent to the continue in C. Redo does not check the loop conditions and restarts the current loop; retry to repeat the loop body from the beginning.

(6) block: it is a code block that can be associated with method calls. Block is a set of code between braces or do... end. You can use the ruby yield statement method to call the associated blocks one or more times. Parameters are enclosed by two vertical bars. Multiple parameters are separated by commas.

(7) The iterator is a method to continuously return elements from a collection object. A large number of blocks are used in the ruby library to implement the iterator. Arrays and ranges belong to collection objects.

(8) process object: pass a piece of code as a parameter to a method. You need to use the proc keyword to create a process object. You can use the created process object. You can use this object through the call method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.