当前位置: 首页 > 图文教程 > 脚本技术 > Ruby > Ruby入门介绍
# parse error def Display(args1="proshea", args2) end # 允许 def Display(args1="proshea", *args2) end # 允许 def Display(args1="proshea", &args) end Show() # 出现在 Show 调用之后是错误的 def Show end
Ruby 也支持 C# params 这样的参数功能, 只是 Ruby 用 * 标识罢了。
def Display(*args) print %Q~#{args.join("-")}~ end # proshea-32-WinForm Display("proshea", 32, "WinForm")
同样的, Ruby 也有类似于 C# delegate 的应用,只是更简单,直接用 & 来表示,并且 Ruby 用一个称为 yield 的关键字来知会解释器执行传入的代码块或者说 Proc object(过程对象?)。
1def Display(&block) 2 if block_given? 3 yield(block) 4 else 5 print %Q~没有传入过程对象~ 6 end 7end 8 9def Show() 10 print %Q~Show 方法调用~ 11end 12 13# 没有传入过程对象 14Display() 15# 在 Display 内部调用 Show 方法 16# 注意起始大括号仍然只能和方法名在同一行 17Display(){ 18 Show() 19}
block_given? 是被定义在内部模块 Kernel 当中的方法,用以表明是否传入了 Proc object。之后,Ruby 用 yield 通知解释器执行传入的 Proc。过程对象也可以带有参数,不同于普通方法的是过程对象的参数是位于一组 | | 之中。可以使用 Proc object 的 call 方法来调用带参数的过程对象。
1class Employee 2 def initialize(username, age, &block) 3 @username, @age, @block = username, age, block 4 end 5 6 def Display(txt) 7 # 虽然 @block 是个实例变量,但在此处一定要加上大括号 8 print "#{@block.call(txt)}: #@username-#@age" 9 end 10end 11 12emp = Employee.new("proshea", 32){ 13 |txt| 14 txt 15} 16emp.Display("context")
BEGIN { print "OnInit(object sender, EventArgs args)\n" } BEGIN { print "OnLoad(object sender, EventArgs args)\n" } print "Running"
BEGIN{ print "OnInit(object sender, EventArgs args)\n" } BEGIN{ print "OnLoad(object sender, EventArgs args)\n" } print "Running"
1i = 0 2while i < 10 3 # 虽然处理循环结构中,但 BEGIN 块内的代码仍然只执行一次 4 BEGIN{ 5 print "OnInit(object sender, EventArgs args)\n" 6 } 7 i += 1 8end 9 10if false 11 # BEGIN 完全不受 if 的影响,只要出现 BEGIN 块就会得到执行 12 BEGIN{ 13 print "OnLoad(object sender, EventArgs args)\n" 14 } 15end 16 17print "Running"
print "OnLoad(object sender, EventArgs args)\n" BEGIN{ print "OnInit(object sender, EventArgs args)\n" } print "Running"
if false END{ # 永远不输出 print "Init" } end END{ # 最后输出 print "Unload\n" } END{ # 先于 Unload 输出 print "Load\n" } # 最先输出 print "Start\n"
val = 5 if false
class Employee @empId end
1module Company 2 @@companyName = "Hello Ruby." 3 4 class Employee 5 def display 6 print "#@companyName" 7 end 8 end 9 10 class Department 11 def display 12 print "#@companyName" 13 end 14 end 15end
1module Company 2 class Employee 3 def display 4 # nil 5 print "#$companyName" 6 $companyName = "Hello Ruby." 7 end 8 end 9 10 class Department 11 def display 12 # Hello Ruby. 13 print "#$companyName" 14 end 15 end 16end
# 当前方法的执行主体 print "#{self}" # NilClass类的唯一实例 print "#{nil}" # TrueClass 类的唯一实例 print "#{true}" # FalseClass 类的唯一实例 print "#{false}" # 当前源文件名 print "#{__FILE__}" # 当前源文件中的行号 print "#{__LINE__}"
1# 属于 Object 的常量 2GroupName = "心守家园" 3 4module Site 5 SiteUrl = "http://www.you2v.com" 6 7 class Sichuan 8 Add = "凉山" 9 # 引用属于 Object 的常量 10 print "#{::GroupName}" 11 end 12end 13 14# 直接引用类名、模块名 15# 引用属于 Object 的模块时可以省略“::” 16print "#{::Site}\n#{Site::Sichuan}" 17# 属于模块的常量 18print "#{Site::SiteUrl}" 19# 属于类的常量 20print "#{Site::Sichuan::Add}"
string val = "value"; string printVal = "value: " + val; // 或者 string printVal = String.Format("value: {0}", val);
val = "Value" printVal = "value: #{val}"
1module Company 2 class Employee 3 # 类变量 4 @@companyName = ".org" 5 # 成员变量 6 @empId 7 8 def setEmpId(val) 9 @empId = val 10 end 11 12 def display() 13 # 省略了大括号 14 print "Company: #@@companyName\n" 15 print "Employee ID: #@empId\n" 16 # 伪变量不能省略大括号 17 print "lines: #{__LINE__}\n" 18 end 19 end 20end 21 22emp = Company::Employee.new 23emp.setEmpId("001") 24emp.display
# 原样输出 # Company: #@@companyName print 'Company: #@@companyName' # 原样输出(包括空格和换行) print ' Company: .org Employee Id: unknow
# 错误 print "Ruby "% 呈现法"" # 应用 % 呈现方法 print %Q#Ruby "% 呈现"#
# 正确 print %Q~Ruby "% 呈现"~ print %Q.Ruby "% 呈现". print %Q*Ruby "% 呈现"* # 在 % 呈现中插入呈现分隔符 print %Q*\* Ruby "% 呈现"* # 错误 print %Q** Ruby "% 呈现"* print %QbRuby "% 呈现"b print %Q<Ruby "% 呈现"<
pat1 = /([\d\w])-([\d\w])/ pat2 = /([\d\w])-\1/
pat1 = /([\d\w])-(\1)0/ pat2 = /([\d\w])-\10/ # 0 print pat1 =~ "1-10" # nil print pat2 =~ "1-10"
评论 (0) All