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"
欢迎转载,转载请注明:转载自 周金根 [ http://zhoujg.cnblogs.com/ ] 

转载于:https://www.cnblogs.com/vingi/articles/2623489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值