.
# bad
begin
# some code
rescue Exception => E
# Some handling
rescue StandardError => 8/># Some handling
end
# good
begin
# some code
rescue StandardError => e
# some Handling
Rescue Exception => E
# some handling
end
Release the external resources that your program obtains in the ensure block.
f = File.Open (' testfile ')
begin
# ... proc
be affect" the value of the variable with the same Nam E in the outer scope.
Blocks for transactions
can use blocks to define a chunk of code this must be run under some kind of transnational
Class File
def self.open_and_process (*args)
f = File.Open (*args)
yield f
f.close
End
file.open_and_process ("Testfile", "R") do |file|
While line = File.gets
puts line
end
Blocks Can be Objects
You can convert a blocks i
How do you write Ruby with the same code as below?
Public Function MakeRequest ($url, $method, $postfields = NULL) { $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $url); curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_httpheader, Array (' Expect: ')); if (' POST ' = = = $method) { curl_setopt ($ch, Curlopt_post, 1); if (!
| http.request(req) }puts res.body
In fact, it should be very simple, that is, how to submit data with Python post.But look at PHP and Ruby's code is not very similar ~Want to have a person to point, personal comparison dish ~ Hope to have a detailed translation, thank you!
Reply content:
This is a Baidu webmaster platform push Case:
Post case:
Post/urls?site=www.nantongzt.comtoken=xxxxxx http/1.1user-agent:curl/7.12.1Host:data.zz.baidu.comContent
Ruby uses threads to implement typical producer and consumer problem code instances.
Sample Code:
Copy codeThe Code is as follows:Require "thread"Puts "ProAndCon"Queue = Queue. new # Use the Queue queue for Thread SynchronizationProducer = Thread. new do10. times do | I |Sleep rand (I) # Let the thread sleep for a peri
This article mainly introduces the Ruby connection using Windows SQL Server database code instance, this article directly gives the implementation code, and gives two implementations and Access database implementation code, the need for friends can refer to the
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Get the currently running method name in Ruby on bigbold
Code Fragment:
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->
Module KernelPrivate
Def
This_method_nameCaller [0]
= ~
/
'([
^
'
] *)
'
/
And
$
1
EndEnd ClassFooDefTest_methodThis_method_nameEndEnd Puts Foo. New. tes
. patents to compute the parity code for hand-held or mechanical devices. So it has to be as simple as possible.
Ruby Version Implementationthe basic principle of Luhn algorithm is very simple: (eg:49927398716)The first step: reverse the credit card number (61789372994)Step Two: Remove the number on the odd position after the reverse, add the sum until S1 (s1=6+7+9+7+9+4=42)Step three: Remove the number on
uses a simpler, incoming parameter (the activity type, the original price) to get the final result.Here the user only needs to know a class (Cashcontext) can be, and simple workshop need to know two classes (Cashfactory Accept_cash method and Cashfactory), that is more thorough encapsulation.Advantages of the policy modelThe algorithms are encapsulated in 1,cashcontext, and these algorithms are all doing the same work, just implementing different, which helps to extract the common functions in
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!= "..."
Traverse_dir (file_path+ "/" +file)
End
End
Else
Puts "File:#{file.basename (File_path)}, Size:#{file.size (File_path)}"
End
End
Traverse_dir (' D:/apache-tom
The ruby method can accept code blocks as parameters, which is a very flexible method and a very good design idea. Suppose there are three methods as shown in 1:
Figure 1
As shown in figure 1, a large number of code blocks of method A, Method B, and method C are identical. Do you need to define three methods? Is there a better way?In the software development fiel
argument to omit the parentheses, there are parameters to use parentheses2, if not necessary, do not use for, and use each to do the loop3, do not use then4. Use three-dimensional operation? : instead of If,else.5, not in if else's internal use? : operator6, use when x then ..., because when x .... The ruby1.9 was deleted.7. Use and | | Do Boolean operation, use And,or to do process control.8, unless and else do not have a9, unless it is to determine the conditions involved assignment operatio
, and the benefit of doing so is to reduce mutual dependence. The first class does not need to deal with anything after it is changed, just give notice to its observers that the specific things are handled by them. For example, one day we want to show time in different forms, or show time in many ways, without having to involve the first class at all.
PS: "Sumbon's Program World" is a good book. At first I wanted to read the book as a ruby tutorial,
Ruby connection uses the SQL server database code instance in windows, rubysql
require 'win32ole'class SqlServer # This class manages database connection and queries attr_accessor :connection, :data, :fields def initialize @connection = nil @data = nil end def open # Open ADO connection to the SQL Server database connection_string = "Provider=SQLOLEDB.1;" connection_string
The test
Confederate Memorial day.)After World War I, the observances were widened to honor the fallen from all American Wars--and in 1971, Congress declared Memorial Day a national holiday.Towns across the country now honor military personnel with services, parades, and fireworks. A National Moment of Remembrance takes place at 3 p.m. At Arlington National Cemetery, headstones is graced with small American flags.This-is-not-to-be-confused with Veterans Day, which are observed on November one to honor m
is still there, that is, the factory method transfers the internal logic judgment of the simple factory to the client code. To add functionality, you would have modified the factory class and now modify the client.The advantage of this is that it overcomes the shortcomings of the simple factory against the "open-closed principle" and preserves the advantages of the encapsulation object creation process.The problem is that the logical branch of judgme
Currently, I am playing the 2048 game. The game logic is simple and is very suitable for new players like me to implement logic. So I chose the best Ruby language to implement the main logic of this game. It's quite simple. It takes about four hours to complete.
Code:
require 'optparse'module HelpHELP_TEXT =
I wrote some tests:
require 'ruby_2048'describe R2048 do before(:each) do @r2048 = R2048.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.