via http://my.opera.com/dstorey/blog/show.dml/701902
据说内部测试版本可以支持下面这些 css 3 属性。:(
A lot of work is going into selectors, and there is a quite extensive list of extra support added to those already implemented in Opera 9.1. New in the latest builds include:
* :root
* :not()
* :nth-child()
* :nth-of-type()
* :first-of-type
* :target (still buggy at present)
:target 见旧日志 http://blog.guoshuang.com/showlog.asp?cat_id=31&log_id=4114
opera 9.10 似乎还是不支持。
test it
[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
As well as those above, the following are implemented in our core engine, but not currently enabled due to various issues:
* :empty
* :nth-last-child
* :nth-last-of-type
* :last-child
* :last-of-type
* :only-child
* :only-of-type
Stuffs:
:root
The :root pseudo-element selects the root of all blocks in the page, ie. the Initial Containing Block. In HTML this is obviously the element
Test stylesheet:
:root {
background-color: #6374AB;
padding: 50px;
}
If the :root selector works the left and right column of the page are blue, and the white middle column is offset by 50 pixels.
irb
irb(main):001:0> “郭爽”.length
=> 6
irb(main):002:0> $KCODE=’utf8′
=> “utf8″
irb(main):003:0> require “jcode”
=> true
irb(main):004:0> “郭爽”.jlength
=> 2
还是 ubuntu 环境为例,命令行运行
irb
打开 交互式 ruby 命令行
89.abs
=> 89
irb(main):002:0> “guoshuang”[0,5]
=> “guosh”
irb(main):003:0> 3.1445.round
=> 3
irb(main):004:0> “guoshuang is great?”.upcase.reverse
=> “?TAERG SI GNAUHSOUG”
89 绝对值
“guoshuang”[0,5]
3.1445 四舍五入
大写并反转字符串
*Ruby是全对象化的:在Ruby中,整数,浮点数,字符串,甚至类和方法都是对象。这里的代码展示了针对这些类型对象的方法调用。
Ruby 类
在Ruby中,所有功能都是通过调用对象上的方法(或操作)实现的。事实上,Ruby中的方法调用就象其它程序语言中的函数或过程调用一样。
就象在所有面向对象程序语言中一样,对象是从类中创建的。Ruby库中提供了许多预构建的类。你可以修改这些类或构建你自己的类。Ruby中的类是使用”class”关键字定义的。类名开始是一个大写字母。类定义以”end”关键字结束。因此,一个Rectangle类的定义可能有如下形式:
class Rectangle
end
为了把方法添加到类,可以使用def关键字。方法的定义也应该以end关键字结束。跟随def关键字和方法名后面就是方法参数。把一个area方法添加到上面的Rectangle类的代码看上去如下所示:
class Rectangle
def area (hgt,wdth)
return hgt*wdth
end
end
对于那些熟悉其它程序语言的用户,他可能注意到一些差别。Ruby并不使用任何花括号来限定类或方法,也不使用分号或其它字符来表示程序语句行的结束。Ruby的目标,根据它的创建者说明,是简单、易用并使编码成为一件”趣事”。谁想记住所有的那些分号?没有意思!在Ruby中,只要你把语句放在一行上,不需要分号或其它代码行结束标记。顺便说一下,在area方法参数周围的括号是不必要的。在默认情况下,Ruby返回一个方法中最后的内容,因此return关键字也可以省略。因此,你可以建立如下简单编码的Rectangle类:
class Rectangle
def area hgt, wdth
hgt*wdth
end
end
尽管上面代码是有效的,但是小括号还是被推荐使用于方法参数表达的,这主要是为了实现较好的可读性。
实例变量和属性
类也可以有实例变量(在一些语言中也称为属性)。例如,由Rectangle类创建的对象应该都有一个高度和宽度。在Ruby中,实例变量不必显式地在类中声明,只是必须在它们的命名中以一个特殊字符来标记和使用。具体地说,所有的实例变量名都以”@”开头。为了实现当调用area方法时,存储矩形实例的高度和宽度,你仅需把实例变量添加到area方法即可:
class Rectangle
def area (hgt, wdth)
@height=hgt
@width = wdth
@height*@width
end
end
更确切地说,当创建一个Rectangle实例时,应该指定高度和宽度,而实例变量在此时才确定。另外,Ruby提供了一种特殊的方法initialize,它允许你建立或准备类的新实例:
class Rectangle
def initialize (hgt, wdth)
@height = hgt
@width = wdth
end
def area ()
@height*@width
end
end
为了创建一个新的Rectangle对象或实例,你要调用标准的Ruby类构造器方法”new”:
Rectangle.new(4,7)
或,你可以使用没有括号的形式:
Rectangle.new 4,7
这个例子创建了一个新的Rectangle对象并且调用了initialize方法,其中传入参数4和7。注意,在下面的代码中添加了height和width方法以便共享高度和宽度信息:
class Rectangle
def initialize (hgt, wdth)
@height = hgt
@width = wdth
end
def height
return @height
end
def width
return @width
end
def area ()
@height*@width
end
end
同样,为了使另外某个方法能够更新或设置一个Rectangle对象的高度和宽度,需要定义其它一些设置方法:
class Rectangle
def initialize (hgt, wdth)
@height = hgt
@width = wdth
end
def height
return @height
end
def height=(newHgt)
@height=newHgt
end
def width
return @width
end
def width=(newWdth)
@width=newWdth
end
def area ()
@height*@width
end
end
译者注 本文中的mutator和accessor相当于其它语言中的setter和getter。
上面的mutator方法(”height=”和”width=”)可能看起来有点神秘,但是它们确实只是一些方法。不要让命名中的等号蒙骗了你。在方法名最后的额外字符对于Ruby并不意味着什么,但是它提高了代码的可读性。请看下列代码:
aRectangle.height=27
在此,一个Rectangle对象的高度正被赋值(改变)。事实上,这仅是一个对Rectangle对象的”height=”方法的调用。
因为授予到一个对象的实例变量的存取权限非常普通,所以Ruby提供了一组关键字来实现一次性定义实例变量和accessor/mutator方法,从而使这些实例变量成为”public”属性。这些关键字是attr_reader,attr_accessor。它们有助于极大地简化代码:
class Rectangle
attr_accessor :height, :width
def initialize (hgt, wdth)
@height = hgt
@width = wdth
end
def area ()
@height*@width
end
end
在上面的示例代码中,attr_accessor给出了Rectangle相应于height和width属性的getter和setter。
用交互式Ruby构建应用程序
现在,你已经知道如何构建一个简单的Ruby应用程序。为了展示Ruby解释器的交互性,让我们启动一个交互的Ruby帮助和控制台工具(使用Ruby安装的fxri工具),见图6。
图6.启动fxri:从Windows开始菜单中打开交互式Ruby工具fxri。
在窗口的右下方是一个交互式的Ruby命令提示符(即”irb(main):001:0>”),它在窗口打开时显示出来。你可以进入到irb命令提示符行中并输入”Rectangle.new(6,5).area()”。之后,你应该看到如下结果:
irb(main):013:0> Rectangle.new(6,5).area()
=> 30
简洁有力!
在Ruby中,类从来不关闭。这意味着,你总是可以在一个现有类上添加或重定义方法。例如,你还可以在你创建的Rectangle类上添加一个circumference方法。在命令提示符上,请逐行输入下列代码:
class Rectangle
def circumference ()
@height * 2 + @width * 2
end
end
图7.输入Rectangle类:把Rectangle类的定义输入到fxri交互式Ruby解释器中,见图中的右下方。
Rectangle类又被定义了一次?不,Ruby解释器知道你正在修改当前的Rectangle类,它把一个新方法添加到现有Rectangle类中。现在,在命令提示符上输入下列一行:”Rectangle.new(2,3).circumference()”。你应该看到类似如下的结果:
irb(main):014:0> class Rectangle
irb(main):015:1> def circumference()
irb(main):016:2> @height * 2 + @width * 2
irb(main):017:2> end
irb(main):018:1> end
=> nil
irb(main):019:0> Rectangle.new(2,3).circumference
=> 10
为了重新定义Rectangle类中的任何方法,例如area方法,只需简单地重新输入具有新的area方法定义的类定义即可:
irb(main):020:0> class Rectangle
irb(main):021:1> def area()
irb(main):022:2> @height*2
irb(main):023:2> end
irb(main):024:1> end
=> nil
irb(main):025:0> Rectangle.new(6,5).area
=> 12
在上面的简单例子中,area方法被重新定义以便总是返回原来高度的2倍。
一个类永远不会被关闭的思想可以应用于你自己定义的类和该语言中的内嵌类中。为了说明问题,让我们把一个area方法添加到String类。在命令提示符中输入下列代码:
class String
def area()
length()
end
end
现在,你在定义一个字符串的”area”方法以返回该字符串的长度。现在,你可以把你的名字作为一个字符串来试用一下这个新方法,见下面代码:
irb(main):026:0> class String
irb(main):027:1> def area()
irb(main):028:2> length()
irb(main):029:2> end
irb(main):030:1> end
=> nil
irb(main):031:0> “Jim”.area
=> 3
以 ubuntu 环境为例,从最简单开始
ubuntu 安装 ruby
sudo apt-get install ruby irb ri rdoc ruby1.8-dev build-essentia
命令行执行:
ruby -v
返回版本
ruby 1.8.4 (2005-12-24) [i486-linux]
ruby -e ‘print “你好!
“‘
-e 直接执行语句,返回
你好!
ruby test.rb
执行 test.rb (文本文件)的内容
Ruby将字符串像数字一样处理.我们用单引号(’…’)或双引号(”…”)将它们括起来.
单引号和双引号在某些情况下有不同的作用.一个由双引号括起来的字符串允许字符由一个前置的斜杠引出,而且可以用#{}内嵌表达式.而单引号括起来的字符串并不会对字符串作任何解释;你看到的是什么便是什么.
ruby -e ‘print “郭爽”+”你好!
“‘
郭爽你好!
ruby -e ‘print “郭爽
“*2′
*2 重复两遍
郭爽
郭爽
ruby -e ‘print “guoshuang”[0].to_s+”
“‘
抽取 “guoshuang” 字符串的第一个字符 g 的 ascii 码。(Rusy 里字符被视为整数)to_s 转换为字符串输出。
103
ruby -e ‘print “guoshuang”[-2].to_s+”
“‘
从后向前取出 n 。
110
ruby -e ‘print “guoshuang”[0,3].to_s+”
“‘
从0开始取3个字符
guo
ruby -e ‘print “guoshuang”[-4,3].to_s+”
“‘
uan
ruby -e ‘print “guoshuang”[2..5].to_s+”
“‘
从2到5
oshu
ruby -e ‘print “guoshuang”[-5..-1].to_s+”
“‘
huang
split的时候发现一个programming ruby里没提到的用法, 刚刚开始还吓了一大跳..
不知道大家知道不, 分享一下先
“aa,bb,cc”.split(/(,)/)
结果是['aa', ',', 'bb', ',', 'cc']
而不是期望中的 ['aa', 'bb', 'cc']
原来正则表达式中的括号搞的鬼
如果写成”aa,bb,cc”.split(/,/)就是会得到期望中的结果了
via rubychina