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
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).
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
First, traversing all files under the folder, output file name
Copy Code code as follows:
def traverse_dir (File_path)
If File.directory? File_path
Dir.foreach (file_path) do |file|
If file!= "." and File!= "..."
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
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,
#scanweb. RB
#用法ruby scanweb.rb www.jb51.net to save the current results in C:\1.txt
Require ' net/http '
Filename= file.new (' C:\1.txt ', "w+")
If $*[0]==nil
Puts "hehe, no input URL"
Else
h = net::http.new ($*[0], 80)
RESP, data = H.get
The Fruit class in the previous section has two real variables, describe the type and state of the fruit. Until a custom inspect method is written for this class, we understand that it does not make a reasonable explanation for a fruit that lacks
Conditional judgments are present in the programming language, and the conditional judgments in Ruby are similar to those in Java, but there is a slight difference
Conditions in ruby conditional judgment:
1 can use comparison operations such as ==,
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
C # implements Ruby's negative index
public class Invertiblelist: list
{public
new T This[int index]
{
get
{
if (Index >= 0) return Base[index];
if (Count + Index = 0)
Base[index] = value;
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