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
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
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 "
Rake I will not introduce it anymore. Ruby's Make is easier to use than Make in many aspects. Unlike Makefile, Rakefile itself is actually a piece of Ruby code, which has many advantages. On the one hand, in Rake, You Can Do Anything Ruby can do
Ruby determines whether a number is a prime number.Prime number. A natural number greater than 1 is called a union number if it cannot be divided by any other natural number except 1 and itself. According to the basic arithmetic theorem, each
Ruby is currently the most flexible and agile programming language. It has special application methods and can bring a pleasant programming environment to programmers. Here we will learn the skills for Ruby to get the current class name.
Using pp (
Speaking of Ruby, of course, we need to mention its regular expression mechanism. As a powerful matching language, regular expressions are increasingly used in different fields, from string verification, matching, to web page extraction. Although
Rails (Ruby on Rails) 3.1 Official version released! This time there was no jump ticket and it was released as scheduled.
The official version provides several new features, including:
◆ Assets Pipeline: A new function Assets pipeline is
RDoc 3.10 is released. RDoc is a file system for Ruby source code. Like JavaDoc, RDoc can read the original file, analyze the syntax information and comment text in the source code, and generate documents in HTML format. Different from JavaDoc, RDoc
RubyMine 4.5 was released. This is a major new version that includes many improvements, including support for MacRuby, Capistrano, Slim, Sass, Rails Engines, development ID, Sinatra, and a series of other improvements.
RubyMine is a brand new IDE
In this chapter, we will discuss more Ruby process control.CaseWe 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 "1 ..
In the subclass, we can modify the object behavior by reloading the parent class method.Ruby> class Human| Def identify| Print "I'm a person. \ n"| End| Def train_toll (age)| If age | Print "CED fare. \ n ";| Else| Print "Normal fare. \ n ";| End|
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
Ruby is a fully object-oriented interpreted scripting language. Its unique nature can help us easily and conveniently compile code. Next let's take a look at some usage of common Ruby libraries.
Ruby regular expressions have many similarities
I. MethodsThe 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
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
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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service