Ruby Experience Accumulation

Source: Internet
Author: User

Conversion of GBK and UTF-8

Use GBK instead of gb2312, because GBK not only contains simplified Chinese, but also traditional Chinese, is a large character set.

# Utf8 to GBK
Def U2g (ucode)
Begin
" # {Iconv. Conv ('gbk', 'utf-8', ucode )} "
Rescue
" # {Ucode} "   # If the conversion fails, do not convert the string and add spaces on both sides of the string to avoid the construction of an incorrect SQL string.
End
End

This is a good way to convert from UTF-8 to GBK encoding. You can use this method to capture information on the network.

Ruby DBI
Using DBI when Ruby interacts with sqlserver is a good choice.
DBI has two important methods. One is the execute method, which returns a result set and the other is the do method. The do method does not return the result set or the affected number of rows, the do method commits a transaction, while the commit method can execute all the transactions submitted using the do method. The commit method returns the number of affected rows.

It should be noted that if you use the do method to insert a piece of data without the commit direct disconnect, this data will be inserted into the database and deleted, because you insert a new data record at this time and find that the self-increasing primary key is left blank, for example, jumping from 3 to 5, so do not forget the commit.

Net: HTTP
The net: http: New Method supports overloading of four parameters, for example
Req = net: http. New 'xxxxxx. com', 80, 'proxy. com', 8080
The third and fourth parameters are used to specify the proxy server.
The second parameter specifies that the port is 80 by default if it is not written.

Some methods of the file class need to include 'win32/file'
Some methods cannot be used because they do not contain Win32/file libraries.Require'Win32/File'
File. archive?'C:/boot. ini'

Use URI: encode to convert the URI.
The binmode mode is used when data read from URI is written locally.

Require ' Open-Uri '
Uri =   ' Http://xxx.com/ .rar '
Data = Open (URI: encode (URI )){ | F | F. Read}
File = File. New URI [URI. rindex ( ' / ' ) +   1 ... URI. Length - 1 ], ' W + '
File. binmode
File < Data
File. Flush
File. Close

Special characters must be replaced when executing SQL statements.Name= "Lee's book"
SQL= "Update tb_files set [name] =# {name. gsub ("'","''")}"

The ruby script suffix is changed to. rbw, so that the console is not displayed during execution.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.