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).
We know the top-level domain, what is the self of the domain defined?
Copy Code code as follows:
Puts self #main
Puts Self.class #Object
We know that when a method is invoked, if there is no object to accept, the default is
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
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
Ruby and Python are too similar, and most of the trade-offs are a reason for personal preference. For example, I feel that Python's "There is" just one way to do it. "Better than Ruby's There are many to do it." It's not just about teamwork, it's
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,
The code is as follows:
Copy Code code as follows:
Require ' date '
Day = Date.new (2008, 2,-1)
End_of_month = Day.strftime ('%d '). to_i
Year_and_month = Day.strftime ('%y%m ')
For today in 1..end_of_month do
P sprintf ('%s%02d ',
Recently because to do a class, so yesterday installed a Windows, feel free to do nothing, want to install a Ruby play.
Official Rubygems source Because some resources are placed on the Amazon S3, so sometimes the ventilation, in Linux I use
In ActiveRecord, you can define the association between model in a convenient declarative way, for example:
Copy Code code as follows:
Class Topic Has_many:p OSTs
Belongs_to:user
End
Has_many and belongs_to are actually
P and puts are the most commonly used methods in Ruby, and many children's shoes may think they are similar and are not noticed when they are used, but they are obviously different when they are carefully studied.
Let me give you an example.
Beginners Ruby, many need to learn, now start to try to write a script using Ruby, which uses a lot of file-related operations, here to organize some. Easy to find again later.
Whether a file or directory exists
Copy Code code as follows:
Recently packaged servers have increased the number of APK packets, each manual RM operation too cumbersome, so spent a few minutes to write a can be specified in the directory according to the last modified time and wildcard matching for bulk
In Ruby we want to implement the method of traversing the specified directory, the online method is also very much, we can refer to reference, such as the traverse.rb file below the contents of:
Copy Code code as follows:
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