1. Notepad++ 执行 ruby脚本的命令(F6):
cmd /k cd $(CURRENT_DIRECTORY) && ruby "$(FULL_CURRENT_PATH)"
2. 代码块
代码块仅仅是一些在花括号或do...end间的代码。
{ puts "Hello" } # this is a blockdo club.enroll(person) # and so is this person.socialize #end
为什么会有两种分隔符呢?其中一个原因是有时候会觉得其中一个比另一个用得更自然。另一个原因是它们的优先级不同:花括号比较do/end对优先级更高。
我们应尽量按照即将成为Ruby标准的用法,单行代码使用花括号,多行代码使用do/end。3. 设置千位分隔符
def commas( x ) str = x.to_s.reverse str.gsub!(/([0-9]{3})/, "\\1,") str.gsub(/,$/, "").reverseend
-
-------------------------------------------------------
正则练习
s = 'Hello, World! Hell, Worse!'s.scan(%r/H(\w+).*?W(\w+)/) { |m| print 'Regex#M1',"\n" p m print m[0], " ", m[1],"\n" print $1, " ", $2, "\n" print 'H',$1,'--','W',$2,"\n"}s.scan(%r/H(?\w+).*?W(? \w+)/) { |m| print 'Regex#M2',"\n" p m print m[0], " ", m[1],"\n" print $1, " ", $2, "\n" ## undefined local variable or method `key' for main:Object (NameError) #print key, " ", value, "\n" print 'H',$1,'--','W',$2,"\n"}s.scan(%r/H(? \w+).*?W(? \w+)/) { |key, value| print 'Regex#M3',"\n" print $1, " ", $2, "\n" print key, " ", value, "\n" print 'H',$1,'--','W',$2,"\n"}s.scan(%r/H(? \w+?).*?W(? \w+?)/) { |key, value| print 'Regex#M4',"\n" print $1, " ", $2, "\n" print key, " ", value, "\n" print 'H',$1,'--','W',$2,"\n"}