首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > Ruby Rails >

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

2012-11-08 
Ruby 10 Minutes(摘要自---Ruby 用户指南)类??简单类 ?ruby class Mammal? ?| ? def initialize? ?

Ruby 10 Minutes (摘要自---Ruby 用户指南)
======类??简单类 ?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.speakBow 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? nilruby> 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? nilruby> repeat(3) { print "foo\n" }foofoofoo? nil ??案例:有while相同作用的迭代器?ruby> def WHILE(cond)? ?| ? return if not cond? ?| ? yield? ?| ? retry? ?| end? nilruby> 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}abc????=====数组?定义数组:方括号里列出元素,不限定类型?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]? 1ruby> 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]? 2ruby> h["2"]? "4"ruby> h[5]? nilruby> h[5] = 10 ? ? # appending value? 10ruby> h? {5=>10, 1=>2, "2"=>"4"}删除ruby> h.delete 1 ? # deleting value? 2??==============字符串?单引号,双引号都可以做字符串定界符?? > "abc" ? > 'abc'?字符串可以跨行"foobar"?双引号的字符串允许字符转义(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 TurningBlog ? ? ? ? :http://1000copy.iteye.comMicroBlog : t.sina.com.cn/1000copyclub page ?:http://www.cdsoftwareclub.com??

热点排行