Class method
The essence of a class method is a single piece method that lives in a single piece of the class. There are three ways to define them, respectively:
# method One
def Myclass.a_class_method end
# Two
class MyClass
def
Algorithm principle:
Compare the adjacent elements. If the first one is bigger than the second one, swap them both.Do the same work for each pair of adjacent elements, from the first pair to the end of the last couple. At this point, the final
Proc Objects
Proc are objects that are converted by blocks. There are four ways to create a proc, respectively:
Sample code
# Law one
inc = Proc.new {| x | x + 1}
Inc.call (2) #=> 3
# method two
inc = Lambda {| x | x + 1}
Inc.call (2) # =>
In a file there are many of the following:
This topic selected D.
....
....
....
And I want to achieve the function is to replace it:
This topic selected D.
.....
.....
.....
This thing looks a little bit simple, but
Scope
Ruby does not have the characteristics of a nested scope (that is, an internal scope that can see an external scope), its scope is completely separate, and once you enter a new scope, the original binding is replaced with a new set of
A constant begins with a capital letter. It should be assigned up to one time. In the current version of Ruby, the re-assignment of constants produces only warnings rather than errors (the Non-ansi version of EVAL.RB does not report this
Global variables begin with $. They can be accessed anywhere in the program. The global variable has a special value nil before initialization.
Ruby> $foo
Nil
ruby> $foo = 5
5
Ruby> $foo
5
Global variables should be used with caution. They are
In general, the definition of the observer pattern should be this: building a clean interface between the source of news that some object has changed and the Consumers O F that news.
The Observer pattern establishes a clean interface between the
Regular match
Speaking of Ruby, of course, the regular expression mechanism of the regular expression as a strong matching language has been more and more used in different fields, from string verification, matching, to Web page extraction and so
Accept user Input
First_Name = Gets.chomp
Capitalize first letter
first_name.capitalize!
Uppercase letters
first_name.upcase!
The letter becomes lowercase
first_name.downcase!
Multi-line output
Language-related settings and strings should not be used in views, models, and controllers. These words should be moved to a language file under the Config/locales.
When the label of the ActiveRecord model needs to be translated, use the
Keep the schema.rb under version control.Use Rake db:scheme:load instead of rake db:migrate to initialize the empty database.Use rake db:test:prepare to update the schema of the test database.
Avoid setting default data in the table. Use the model
When you need to add one or more actions to a RESTful resource (do you really need it?) ), using the member and collection routing.
# get
' subscriptions/:id/unsubscribe '
resources:subscriptions
# good
resources:subscriptions do
Some difficulties in learning ruby language learning comprehension
The Ruby language is a relatively simple language because all of its design principles are object-oriented, so it's especially handy to use. Of course, the main reason is
Recently doing a scan code login function, for this I also searched the internet about the implementation of the scan login. When this function was completed, I decided to organize the whole realization, so that I can see it later and also
Ruby's garbage collectorIn Ruby, almost everything you do requires memory.The job of the Ruby garbage collector is to manage memory and eventually release objects that are no longer in use. But this would sacrifice a bit of performance, and it would
#将ARGV [0] convert to regular expression typePattern= regexp.new (argv[0])#Devise Gem PackDevise is one of the most widely used authentication gem packages in Ruby. Devise provides us with complete features from registration to login, from forgotten
Ruby saves the data queried by mysql to excel and rubymysql
Require "win32ole" require 'pathname' require 'mysql2' excel = WIN32OLE. new ('excel. application ') excel. visible = true # in case you want to see what happensclient = Mysql2: Client. new
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.