IronRuby - 快速在半小时学习Ruby基础知识

  在前面几篇blog中我介绍了一些IronRuby相关的内容,由于IronRuby是Ruby在.Net的一种实现而已,所以其基本语法和使用都类似Ruby,那么要想学会使用IronRuby,我们首先就要先快速了解如何使用Ruby,本篇主要介绍一下Ruby的一些基础知识,这也是我这几天主要用到的东西,希望对初学者有所帮助。

Variables

定义变量很简单,只要写上一个小写的编码名,后面跟上等号和值就行了,如

str = "你好"
num = 1
arr = [1, 2, 3]

Conditions

  • if XXX then  XXX  elseif XXX then  XXX  end
  • title = "登录" if (title == "")
  • unless (password == "") the  XXX  end

Loops

  • while XXX do XXX  end
  • until XXX do  XXX  end
  • for item in XXX do  XXX  end
  • 10.times do |i|  print i end
  • a.each do |item|  puts item  end

Error Handling

begin
# something risky
rescue SomeException
XXX
rescue Exception
XXX
rescue
XXX
end

Methods

def add(a, b)
a + b
end


  • 方法返回值可以通过return XXX,或者直接在方法最后写上需要返回的值
  • result1 = add(1, 2)   或者不加括号   result2 = add 1, 2

Classes

class TestApplication
  def initialize #构造函数
    self.runing = false #给属性赋值
  end

  attr_accessor :runing  #属性 attr_reader attr_writer

  @@instance = nil #类变量
  @instance_variable = 0 #实例变量

  def self.instance
    @@instance = self.new if @@instance == nil
    @@instance #函数返回单例
  end


#实例方法
 def run

     return if self.runing
XXX
   self.runing = true
  end

#类方法
def self.say_hello
puts "Hello"
end



end


  • 生成类 TestApplication.new
  • 使用super调用父类方法,如果参数一样,则可以不传人参数

Using Code from Other Files

  • $LOAD_PATH << 'D:/GZJ/OpenExpressApp/Tool/OpenTest/dll'
    require "Microsoft.VisualStudio.TestTools.UITesting.dll"
  •   require "Utils/find_control_helper.rb"

 

推荐:你可能需要的在线电子书

敏捷个人sina微刊:http://kan.weibo.com/kan/3483302195814612

 欢迎转载,转载请注明:转载自敏捷个人网站

 

posted on 2010-06-24 11:23  周 金根  阅读(3923)  评论(7编辑  收藏  举报

导航