I suddenly found that Ruby is also good, and I want to learn it.
Introduction
This is a short Ruby entry, and it takes only 20 minutes to complete. Here we assume that you have installed Ruby. If you have not installed Ruby, visit the official Ruby
A running
Program An unexpected problem occurs. A file to be read does not exist. When you want to store some data, the disk is full. You may enter inappropriate data.
Ruby> file = open ("some_file ")Err: (eval): 1: In 'open': no such file or
I. Methods
The ruby method definition allows you to set default values for parameters. However, parameters without default values (* and &) cannot be followed by parameters with default values &), that is to say, the following method definition is
Let's write a function to calculate the factorial. The mathematical definition of the factorial is as follows:
N! = 1 (when n = 0)= N * (n-1 )! (Other Cases)
In Ruby, you can write as follows:
CopyCode The Code is as follows: def fact (N)
If
Private
Private functions can only be called in the context of the class and subclass, and can only be accessed through self.
This means that the private function can only be accessed within the object.
The access permission of the object
In the last content, we wrote the CHECKOUT processing. This time, we will display the shopping cart and order on a page, so that users can easily see their shopping situation when placing orders.
1. to modify the checkout. rhtml file, add the
# Encode the string by UBBDef ubb_encode (source_str)Source_str.scan (/ /) Do | v |If v. match (/src = ["']. *? ["']/)! = NilSource_str = source_str.sub (v, '[img' + v. match (/src = ["']. *? ["']/) [0] +'/] ')EndEndSource_str.scan (/ /) Do | span
The iterator was not invented by Ruby. it is widely used in various object-oriented languages. this is also true in Lisp. even so, the concept of iterator is not familiar to many people, so we will give a more detailed introduction here.
You know,
In this chapter, we will discuss more Ruby process control.
Case
We use case statements to test ordered conditions. As we have seen, this is close to the switch in C and Java, but it is more powerful.
Ruby> I = 8Ruby> case I| When 1, 2 .. 5| Print "
Previously, we mentioned that Ruby has no functions, but only methods. There are actually more than one method. In this section, we will introduce access control (access controls ).Think about what happens when we define a method in the "highest
A real variable starts with @ and Its range is limited to the self object. two different objects can have real variables with different values even if they belong to the same class. from outside the object, real variables cannot be changed or even
Local variables start with lowercase letters or underscores (_). Local variables do not include nil values before initialization like global variables and real variables.Ruby> $ fooNilRuby> @ fooNilRuby> fooERR: (eval): 1: undefined local variable
PrivatePrivate functions can only be called in the context of the class and subclass, and can only be accessed through self.This means that the private function can only be accessed within the object.The access permission of the object instance
Ruby is a powerful object-oriented scripting language that allows you to easily and quickly perform object-oriented programming. sometimes using formal object-oriented languages such as Smalltalk, Eiffel, and C ++ to develop small projects seems a
First, you must check whether Ruby has been installed. At the shell prompt ("%" here, so do not enter % ),% Ruby-v(-V tells the interpreter to print out the Ruby version) and press Enter. If Ruby is installed, you will see information similar to the
Let's look at a more interesting program. This time we will test whether a string matches the description produced by concise pattern encoding.In these pattern modes, some characters or character combinations have unique meanings, including:Copy
Now let's take a look at the code of some previous sample programs.The following example is shown in the simple example section.Def fact (n)If n = 01ElseN * fact (n-1)EndEndPrint fact (ARGV [0]. to_ I), "\ n"Because this is the first explanation, we
1. send messages to the object We can directly send messages to objects:Ruby codeCopy codeThe Code is as follows:Class HelloWorldDef say (name)Print "Hello,", nameEndEndHw = HelloWorld. newHw. send (: say, "world ")We usually use hw. say ("world"),
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:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.