您的位置:首页 > 编程语言 > Ruby

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

2010-06-24 11:23 555 查看
  在前面几篇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 endtitle = "登录" if (title == "")unless (password == "") the XXX end

Loops

while XXX do XXX enduntil XXX do XXX endfor item in XXX do XXX end10.times do |i| print i enda.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"[b]

[/b][b]推荐:你可能需要的在线电子书[/b]敏捷个人sina微刊:http://kan.weibo.com/kan/3483302195814612欢迎转载,转载请注明:转载自敏捷个人网站
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: