A real variable starts at the @, its scope is limited to the self object. Two different objects, even if they belong to the same class, can have real variables of different values. From outside the object, the real variable cannot be changed or even
We always want to classify unknown events. When it happens, passing a piece of code as a parameter to other methods is the easiest way to do it, which means we want to work with the code like we do with the data.
A new process object can be created
Ruby's modules are very similar to classes except:
Modules may not have entities
Modules may not have subclasses
Modules are defined by Module...end.
As a matter of fact... The module's ' Module class ' Is the class ' class ' parent class. Do
The behavior of an entity depends on its class, but many times we know that a particular entity requires specific behavior. In many languages, we have to get into the trouble of defining another class, even if it's just going to be materialized once.
First, you have to check if Ruby is installed. In the shell prompt entered prompt (here is expressed as "%", so don't typing%), typing
% Ruby-v
(-V tells the interpreter to print out Ruby version) and then knocks in the carriage. If Ruby is
The built-in support for regular expressions is usually limited to scripting languages such as Ruby,perl and awk, which is a disgrace: Although regular expressions are mysterious, they are a powerful tool for text processing. It is very different to
Method_missing is a common technique for Ruby metaprogramming (metaprogramming). The basic idea is to invoke a method that does not exist by implementing a callback. A typical example is the dynamic lookup of a ActiveRecord (the Dynamics Finder).
Complexity of Time: Theta (n^2)
Bubble sort
Copy Code code as follows:
Def Bubble_sort (a)
(a.size-2). Downto (0) do |i|
(0..I). Each do |j|
A[J], a[j+1] = a[j+1], a[j] if A[J] > a[j+1]
End
End
Return a
End
Algorithm class operations, using the partition method, merge sort.
#encoding: Utf-8 #author: Xu Jin, 4100213 #date: Oct #MergeSort #to sort an array by using MergeSort algorithm # Example output: #The original Array is:[4,-----------------, 4,
The pseudo code in the introduction of the algorithm is rewritten, coupled with the constructor of the solution of the first question in the introductory lesson practice.
Copy Code code as follows:
#encoding: Utf-8
=begin
Author:xu Jin
#建立一个222. rb file and enter the character file = File.Open ("222.rb", "w+") file.puts "123\NWADWA\N12124124\NDWDW" File.close #输出222. RB Content Fil
E.open ("222.rb", "r+") do |file|
While line = File.gets puts line end #直接用IO操作文件 Io.foreach
Code that is annotated in a Ruby line is ignored at run time. Single-line comment # characters begin, they are from # to end of line:
#!/usr/bin/ruby-w
# is a single line comment.
Puts "Hello, ruby!"
When the above program executes, the
IteratorDefined
A Ruby iterator is, simple A, can invoke a block of code.
Block is typically followed by method, and the code in the block does not necessarily execute
If there is a yield in method, the code in its block is executed
The block in Ruby is generally translated into blocks of code, which seems a bit strange at first because there is no such thing in many languages. Actually, it's not bad.first-class function and Higher-order function
The first-class function and
The ruby thread implements the classic producer consumer problem, using the queue class in Ruby to implement thread synchronization issues.
Copy Code code as follows:
Require "thread"
Puts "Proandcon"
Queue = Queue.new
For example, consider the following regular expression for the matching date:
Copy Code code as follows:
/\a ((?: 19|20) [0-9]{2}) [\-\/.] (0[1-9]|1[012]) [\- \/.] (0[1-9]| [12] [0-9]|3[01]) \z/
With the RE, regular expressions can
One, Space and tabIndent with spaces (95.364%)Use tab indent (4.636%)Based on 1,001,987 submissionsSecond, maximum line width80 characters (92.618%)120 characters (5.893%)150 characters (1.489%)Based on 1,057,374 submissionsThree, operator, colon,
In an expression, when Ruby sees a name like a, it needs to decide whether a is a local variable reference or a call to method a with no parameters. Ruby uses a heuristic approach to judge this situation. When ruby resolves the source code file, it
Under Windows, we all know where the hosts file is, but in the Ubuntu system, there are other systems, such as the Android phone, and maybe there aren't as many small software that you can easily modify, so let's write it ourselves.
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.