方法一:
def URLDecode(str)
str.gsub!(/%[a-fA-F0-9]{2}/) { |x| x = x[1..2].hex.chr }
end
def URLEncode(str)
str.gsub!(/[^\w$&\-+.,\/:;=?@]/) { |x| x = format("%%%x", x[0]) }
end
链接:http://www.2maomao.com/blog/2-ruby-cookies-en-de-code/
方法二:
采用ERB::Util.url_encode()
其中在url_encode 处理如下
def url_encode(s)
s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
end
在正则表达式中:
[^a-zA-Z0-9_\-.]表示匹配非a-z 和A-Z 包括0-9 以及下划线和-中横线 的字符,
$& 全局变量,匹配到的字符串存放在 $& 变量中
uppack 是字符对象中的一个方法:是拆字符到数组的函数,
str.unpack( format ) -> anArray
Decodes str (which may contain binary data) according to the format string,
returning an array of each value extracted. 。。
其中C表示按字符拆。还有参数如下:
Directives for String#unpack
Format Function Returns
A String with trailing nulls and spaces removed. String
a String. String
B Extract bits from each character (msb first). String
b Extract bits from each character (lsb first). String
C Extract a character as an unsigned integer. Fixnum
c Extract a character as an integer. Fixnum
d Treat sizeof(double) characters as a native double. Float
E Treat sizeof(double) characters as a double in little-endian byte order. Float
e Treat sizeof(float) characters as a float in little-endian byte order. Float
f Treat sizeof(float) characters as a native float. Float
G Treat sizeof(double) characters as a double in network byte order. Float
g Treat sizeof(float) characters as a float in network byte order. Float
H Extract hex nibbles from each character (most significant first). String
h Extract hex nibbles from each character (least significant first). String
I Treat sizeof(int) 1 successive characters as an unsigned native integer. Integer
i Treat sizeof(int) 1 successive characters as a signed native integer. Integer
L Treat four1 successive characters as an unsigned native long integer. Integer
l Treat four1 successive characters as a signed native long integer. Integer
M Extract a quoted-printable string. String
m Extract a base64 encoded string. String 能把base64直接解出。
N Treat four characters as an unsigned long in network byte order. Fixnum
n Treat two characters as an unsigned short in network byte order. Fixnum
P Treat sizeof(char *) characters as a pointer, and return len characters from the referenced location. String
p Treat sizeof(char *) characters as a pointer to a null-terminated string. String
S Treat two1 successive characters as an unsigned short in native byte order. Fixnum
s Treat two1 successive characters as a signed short in native byte order. Fixnum
U Extract UTF-8 characters as unsigned integers. Integer
u Extract a UU-encoded string. String
V Treat four characters as an unsigned long in little-endian byte order. Fixnum
v Treat two characters as an unsigned short in little-endian byte order. Fixnum
X Skip backward one character. ---
x Skip forward one character. ---
Z String with trailing nulls removed. String
@ Skip to the offset given by the length argument.
其中参数带个上述格式,还可在格式后跟个数字or*,表示做几次拆解,
我靠,日本人搞的东西咋这么复杂呢,以为是unix啊,@#¥%!@#¥!,慢慢看吧。
自我感觉比c难学。不过他这一步就可以做好多事情,很好很强大啊。