ITeye博客

Ruby 10 Minutes (摘要自---Ruby 用户指南)

1000copy · 2011-03-31 11:27 · 编程语言 · Markdown

======类 

 

简单类

 

ruby> class Mammal

   |   def initialize

   |   end

   |   def breathe

   |     print "inhale and exhale\n"

   |   end

   | end

  nil

 

继承,用<

ruby> class Cat<Mammal

   |   def speak

   |     print "Meow\n"

   |   end

   | end

 

创建对象

 

ruby> pochi = Cat.new

  #<Cat:0xbcb90>  

 

 

调用方法

 

ruby> pochi.speak

Bow Wow

  nil  

 

 

成员变量

全局变量  $          

实变量    @          

局部变量    小写或者_ 打头

常量      大写字母开头

 

存取器

 

Shortcut缩写          Effect等同于  

attr_reader :v        def v; @v; end  

attr_writer :v        def v=(value); @v=value; end  

attr_accessor :v      attr_reader :v; attr_writer :v  

attr_accessor :v, :w  attr_accessor :v; attr_accessor :w  

 

 

======方法

 

参数应在一对括号内

 

object.method(arg1, arg2)

 

 

括号可以省掉.

 

object.method arg1, arg2

 

 

特殊变量self;

 

self.method_name(args...)

 

 

和这一样

 

method_name(args...)

 

特殊成员方法inspect方法,类似C#的ToString

 

ruby> class Fruit

   |   def inspect

   |     "a fruit of the " + @kind + " variety"

   |   end

   | end

  nil

ruby> f2

  "a fruit of the banana variety"  

 

构造方法initialize,用super调用父类

 

ruby> class Cat<Mammal

   |   def initialize

   |     super

   |     print "Meow\n"

   |   end

   | end

 

=====Cool 迭代之yield,retry

 

yield 让流程可以使用Block

 

ruby> def repeat(num)

   |   while num > 0

   |     yield

   |     num -= 1

   |   end

   | end

  nil

ruby> repeat(3) { print "foo\n" }

foo

foo

foo

  nil

 

 

案例:有while相同作用的迭代器

 

ruby> def WHILE(cond)

   |   return if not cond

   |   yield

   |   retry

   | end

  nil

ruby> i=0; WHILE(i<3) { print i; i+=1 }

012   nil

 

retry语句从头执行迭代.这是一个死循环,到2就重新执行

 

ruby> for i in 0..4

   |   print i

   |   if i == 2 then

   |     retry

   |   end

   | end;

 

===== 迭代

 

 

字符串迭代

 

each_byte

 

> "abc".each_byte{|c| printf "%c", c}

abc

 

 

each_line.

 

ruby> "a\nb\nc".each_line{|l| print l}

a

b

c 

 

 

 

=====数组

 

定义数组:方括号里列出元素,不限定类型

 

ruby> ary = [1, 2, "3"]

  [1, 2, "3"]  

 

 

数组加

 

ruby> ary + ["foo", "bar"]

  [1, 2, "3", "foo", "bar"]

 

数组乘

 

ruby> ary * 2

  [1, 2, "3", 1, 2, "3"]  

 

 

访问

ruby> ary[0]

  1

ruby> ary[0..1]

  [1, 2]

ruby> ary[-2]

  2

 

 

数组字符串转换

 

ruby> str = ary.join(":")

  "1:2:3"

ruby> str.split(":")

  ["1", "2", "3"]  

 

=============哈希表

 

定义

ruby> h = {1 => 2, "2" => "4"}

  {1=>2, "2"=>"4"}

使用

ruby> h[1]

  2

ruby> h["2"]

  "4"

ruby> h[5]

  nil

ruby> h[5] = 10     # appending value

  10

ruby> h

  {5=>10, 1=>2, "2"=>"4"}

删除

ruby> h.delete 1   # deleting value

  2

 

 

==============字符串

 

单引号,双引号都可以做字符串定界符

 

  > "abc"

  > 'abc'

 

字符串可以跨行

"foo

bar"

 

双引号的字符串允许字符转义(Escape),用#{}内嵌表达式.

 

 >"\n"

 >"name="1000copy"

 > #{name}"

 

单引号字符串保持原字符串

 

ruby> print 'a\nb\n'

a\nb\nc

 

字符串乘

 

> "foo" * 2

  "foofoo"

 

抽取字符,返回ASCII

 

ruby> word[0]

  102            # 102 is ASCII code of `f'

ruby> word[-1]

  111            # 111 is ASCII code of `o'  

 

提取子串:

 

ruby> herb = "parsley"

  "parsley"

ruby> herb[0,1]

  "p"

ruby> herb[-2,2]

  "ey"

 

检查相等:

 

ruby> "foo" == "foo"

  true

 

 

正则表达式

 

// 表达为regexp

/\w/  字母或数字

/\s/  非空

 

optimize my sight——from Refactor to DB Turning

Blog         :http://1000copy.iteye.com

MicroBlog : t.sina.com.cn/1000copy

club page  :http://www.cdsoftwareclub.com

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。