Skip to content

Instantly share code, notes, and snippets.

@jackywyz
Created December 15, 2011 03:29
Show Gist options
  • Save jackywyz/1479701 to your computer and use it in GitHub Desktop.
Save jackywyz/1479701 to your computer and use it in GitHub Desktop.
Ruby笔记
def foo
f = Proc.new { return "return from foo from inside proc" }
f.call # control leaves foo here
return "return from foo"
end
def bar
f = lambda { return "return from lambda" }
puts f.call # control does not leave bar here prints "return from lambda"
return "return from bar"
end
puts foo # prints "return from foo from inside proc"
puts bar # prints "return from bar"

###数据类型

  • 5, 5.0, 1e3, 数组[] , 区间(1..3,1...3) to_a,字典{:a=>2}
  • 没有元组

###表达式关键字

  • if a then b else c
  • a ? b:c
  • for i in 1..10 [方法break , next & redo & retry]
  • while end
  • unless == if not
  • until end
  • case when end
  • @ @@ $ ::

###函数()

  • b = ->(a){print a} / b = proc{|x| print x} / lambda {|x| print x}
  • def a(b=2); def a(*args);

###模块

###module m

  • require / load 加载文件
  • include / extend 加载模块

###类

class Person < Human
  public :say
 def initialize( name, age=18 )
 
 end
 attr_accessor :name
  def say(@name)
  end
end 
  • 对象Person.new()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment