2007年1月19日星期五

Regular Expression In Ruby Language

Four ways of writing regular expression in ruby:
  1. /something/
  2. Regexp.new("something")
  3. Regexp.compile("something")
  4. %r{something}

Regexp::MULTILINE m
Normally, a regexp matches against a single line of a string. This will cause a regexp to treat line breaks like any other character.

Regexp::IGNORECASE i
Makes matches case-insensitive


Regexp::EXTENDED x
This modifier lets you space out your regular expressions with whitespace and comments, making them more legible.

一篇关于ruby中正则表达式的文章翻译
原文出处: http://www.regular-expressions.info/ruby.html

Ruby中正则表达式的修饰符(译)

在Ruby中使用正则表达式 Ruby支持正则表达式作为其一项语言特性。在Ruby中,正则表达式被写作 /模版/修饰符 的形式,这里的"模版"就是正则表达式本身,而"修饰符"则是一系列不同选项字符的标识,另外,"修饰符"部分是可选的。这个语法特点从Perl借鉴而来。 Ruby支持如下的修饰符: /i 使正则表达式大小写不敏感(正则表达式本身是大小写敏感的) /m 使"."匹配新行。Ruby使用/m,然而Perl和其他很多编程语言使用/s来实现"点(.)匹配新行"。 /o 它将致使任何的 #{...}替换符在一个特殊的正则表达式中仅仅被执行一次,它在第一次会被求值。然而,如果没有用/o,替换符将会每次都执行,并且产生一个Regexp对象。
你能同时使用如上的修饰符象这样: /rgex/is
在Ruby中,^字符号和$符号总是匹配新行的前后。Ruby没有修饰符能够改变这个特点。使用\A 和 \Z 可以匹配在字符串的始端或者末端。 因为正斜杆界定了正则表达式的长度,所以任何出现在正则表达式中的正斜杆都将被除去。例如,在ruby中,正则表达式 1/2 被写做 /1\/2/ (这里隐藏了含义,即反斜杆\,通常被我们成为转义符)

2007年1月14日星期日

2007,昆明,失去的神经

似乎很久没有在纸上写过东西了,我们这样的人在过去都被成为网虫。网络发达的今天,网虫一词逐渐淡去,大家都默认了blog这样的公开日志,而blog也成为无数八卦的重要来源。自己曾在多个blog或space上面写东西,算起来也有些时日,自从搬到google blogger,都疏远了自己那些多少有些神经的灵魂,仿佛快要和那列火车脱轨,还好不是出轨:)
算不上什么文学青年,只是略喜文字一二,目前把每天写各种字符作为生存的饭碗,而且居然能用奇怪的ascii字符拼凑出写所谓的软件,不知是喜是悲。
悲?何来?把吉他拿在手上,已经找不到哪个和弦可以作为我心情的起点,心里的江湖一片乱麻麻,昨天的请柬,明天朋友的小孩,已经印证着偶还住在单身公寓的某一层。近日,心情来的较乱,盛上半杯干红,点上一根香烟,吞云吐雾之间,略微感觉一次惬意,小资了?也许。
买了张SUZANNE VEGA的专辑,只因CD壳子封面有“民谣诗人”四字。“民谣”?“诗人”?高晓松?郁冬?哈哈,一堆关系在脑子里不停的转来转去,又有什么关系呢?买了就买了,好像爱了就是爱了,散了还是散了,忘了就是忘了。
曾经一度假设在某一个梦好了的场景里,我们重逢,相遇,再分离。可终究在2007到来的日子里,我依然在等候,显然的,我开始了绝望,心死。又一年,把上一年留下的枯叶都卷入尘土,都给新来的年累积着思念,累积着财富。财富就是我们有过的每一天,平淡也好,激烈也好,更甚者是平庸也好,都一样是每一秒,每一刻,每一天,每一年,年年岁岁,岁岁年年,我们依旧在成长里和周围的一切相遇、分离、相遇......

2007年1月13日星期六

Case of String


Here,there are methods about case of string in Ruby language.
1.String#upcase
2.String#upcasedowncase
3.String#swapcase
4.String#capitalize
5.String#capitalize_first_letter

All four methods have corresponding methods that modify a string in place rather than creating a new one: upcase!, downcase!, swapcase!, and capitalize!. Assuming you don't need the original string, these methods will save memory, especially if the string is large.



To change the case of specific letters while leaving the rest alone, you can use the TR or TR! methods, which translate one character into another:

'LOWERCASE ALL VOWELS'.tr('AEIOU', 'aeiou')
# => "LoWeRCaSe aLL VoWeLS"


2007年1月10日星期三

Ruby Continue~~


In recipe 1.4, there is a example:
"Three little words".split(/\s+/)
#=>["Three","little","words"]
"Three little words".split(/(\s+)/)
#=>["Three"," ","little"," ","words"]

Here,when I firstly looked at,I confused. But then I think about it carefully,I got way~! In regular expression /(\s+)/, parentheses make spaces stored. Because parentheses in regular expressions, it make the characters of including by parentheses stored in memory,then u can use \1 to reference it after parentheses.



A important tip in recipe 1.5. Special characters are only interpreted in strings delimited by double quotes, or strings created with %{} or %Q{}, but single quotes and %q{} are not.

puts "foo\tbar"
#=>"foo bar"
puts %{foo\tbar}
#=>"foo bar"
puts %Q{foo\tbar}
#=>"foo bar"

puts 'foo\tbar'
#=>"foo\tbar"
puts %q{foo\tbar}
#=>"foo\tbar"




In Recipe 1.6, I got three ways to convert between charactors and values.
1.use ? charactor
?a
#=>97
2.use to access a element of array
'a'.[0]
#=>97
3.from ascii to char,use #chr method
97.chr
#=>"a"

2007年1月9日星期二

Symbol and String


A Symbol is about the most basic Ruby object you can create. It's just a name and an internal ID. Symbols are useful becase a given symbol name refers to the same object throughout a Ruby program.In Rails,we usually use this point,such as ":action=>welcome" and so on. If you wanna get the corresponding symbol of a given string,String.intern will be.

name ="allen"

puts :allen
#=>"allen"
puts name
#=>"allen"

puts :allen.object_id
#=>190498
puts name.intern.object_id
#=>190498
puts name.object_id
#=>21723660




In Rails, we can see the symbol around web application code.Let's think about it!
Hem.......because symbol don't allocate another memory,it doesn't like string.

puts "allen".object_id
#=>21723550
puts "allen".object_id
#=>21723490
puts :allen.object_id
#=>190498
puts :allen.object_id
#=>190498

Substituting variables into an Existing String

From last recipe,we know a way of substituting variables into string. Here, there are some ways again.



1. Printf-style string format
var = 'My name is %s.'
var % 'Allen'
#=> "My name is Allen."
'To 2 decimal places: %.2f' % Math:PI
#=> "To 2 decimal places: 3.14"
'Zero-padded: %.5d' % Math:PI
#=> "Zero-padded: 00003"




2.Use ERB
require 'erb'

template = ERB.new %q{It is <%= var%>!}
var = "here"
# When var is binded to template,just like following
puts template.result(binding)
#=> print "It is here"

2007年1月8日星期一

Purposes of Schedule

From last week to this,I made a plan of our team for project management in 2007.It a hard process,really,I think.You need take care everything about project,such as collabaration,communication,timestamp,customer feedback and so on. And by accident, the chapter 2 is written about schedules.For me, right~!



First,I review the three purposes about schedule:
1.To make commitments about when things will be done.
2.To encourage everyone who's contributing to a project to see her efforts as part of a whole, and invest in making her pieces work with the others.
3.To give the team a tool to track progress and to break work into manageable chunks.




For three purposes, it's very hard to understand when I looked at them firstly.The first, time is focused on. It means we focus on what will happen and what will change,or what will delay.The second,it emphasize function of schedules.It means scheduls inspirit person who is in team,and in another way,it is a forcing functions for project.The last,it's easy to understand as long as whoever have expriences of IT project.