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
Let's write a function that calculates the factorial. The mathematical definition of factorial is as follows:
N! = 1 (when n==0)
= n * (n-1)! (Other cases)
In Ruby, you can write this:
Copy Code code as follows:
def fact (N)
If
First, the methodRuby's method definition allows you to set a default value for a parameter, but you cannot have a parameter without a default value after an argument with a default value (Allow * and &), meaning that the following method definition
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
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
Longest public subsequence, LCS, dynamic programming implementation.
#encoding: Utf-8 #author: Xu Jin, 4100213 #date: Nov, #Longest-commom-subsequence #to find a longest commom subse Quence of two given character arrays by using LCS algorithm #
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 string in the Ruby language is mutable, unlike a string in Java, C #, which is immutable. Like what
Copy Code code as follows:
str1= "ABC"
str2= "ABC"
In Java, for literal strings, a table is maintained inside the JVM,
In terms of technology alone, Ruby is really cool, making the programmer's job a fun and easy task!The following code shows how to find the prime number within 100:
Copy Code code as follows:
Using System;
Namespace Mersenne
{
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 ',
The new version of Will_paginate has abandoned this practice, instead of using the gem approach, this article through the establishment of a foobar application to understand the use of will_paginate.
==============================
C:\>rails foobar-
Like Ruby Metaprogramming, puppet and chef use a lot of Ruby's language features to define a new deployment language.Share a few in the actual project used in the scene, limited capacity, if there is a better program, please leave a message to me:
First, let's take a look at Newrelic's report.
Average response time of the last 24h
Those pages with high traffic (Action)
Access to several Action cases:
Topicscontroller#show
Userscontroller#show (more miserable, mainly GitHub API
In older versions of Ruby, the return value of an assignment statement is the return value of the method that sets the property. In Ruby1.8, the value of an assignment statement is always the value of the parameter and the return value of the method
Some time ago to see a lot of people write the next sister script, I also write a
Module Commonhelper
require ' Nokogiri '
require ' Open-uri '
def down_load_xmz
site_url = ' http:// Www.xxx.com "for
index_page in 1..141
In the rails principle there is the idea of simplifying controller, enriching model. A simple inheritance optimization is shown below with an example. Especially when you have a change self variable in your method.
Suppose we have the following
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.
It is early to know that Ruby has 4 methods of equality, namely: "= =", "= =", "Equal?" and "Eql?", the usual procedures are used, but feel a lack of in-depth understanding of it, reading the rails part of the source code when you can not grasp the
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:
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