首页 / RUBY / 有趣的Ruby-学习笔记1
有趣的Ruby-学习笔记1
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了有趣的Ruby-学习笔记1,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4416字,纯文字阅读大概需要7分钟。
内容图文
Ruby类
类定义
#!/usr/bin/ruby class Sample def hello puts "Hello Ruby!" end end # 使用上面的类来创建对象 object = Sample. new object.hello注意:无参数的函数调用可以省略()
初始化方法
class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end end
Ruby变量
- 一般小写字母、下划线开头:变量(Variable)。
- $开头:全局变量(Global variable)。
- @开头:实例变量(Instance variable)。
- @@开头:类变量(Class variable)类变量被共享在整个继承链中
- 大写字母开头:常数(Constant)。
变量(就是 局部变量)
变量的打印
a=1 b=2 puts "a: #a" puts "b: #b"打印结果
a: #a b: #b正确的写法
a=1 b=2 puts "a: #{a}" puts "b: #{b}"打印结果
a: 1 b: 2
变量的生存周期
class Test2 a=1 b=2 def printVar() puts "a: #{a}" puts "b: #{b}" end end hellotest = Test2.new hellotest.printVar()输出
test.rb:5:in `printVar‘: undefined local variable or method `a‘ for #<Test2:0x00000002cf2248> (NameError) from test.rb:10:in `<main>‘正确的写法
class Test2 def printVar(a,b) puts "a: #{a}" puts "b: #{b}" end end hellotest = Test2.new hellotest.printVar(1,2)输出
a: 1 b: 2
变量的传递
简单类型是值拷贝(字符串也是简单对象,这点跟java不一样)class Test2 def testPass(a,b) puts "before add : a: #{a} b: #{b}" addVar(a,b) puts "after add : a: #{a} b: #{b}" end def addVar(a,b) a += 1 b += 2 end end hellotest = Test2.new hellotest.testPass(1,2)输出
before add : a: 1 b: 2 after add : a: 1 b: 2复杂对象是对象引用
class Obj1 def initialize(a) @a=a end def printVal() puts "a: #@a" end def setA(a) @a=a end def getA() return @a end end class Test2 def testPass() testobj = Obj1.new("hello") a = testobj.getA() puts "before add : a: #{a}" addVar(testobj) a = testobj.getA() puts "after add : a: #{a}" end def addVar(obj) obj.setA(obj.getA() + " world") end end hellotest = Test2.new hellotest.testPass()输出
before add : a: hello after add : a: hello world
实例变量
实例变量的打印
实例变量的生存周期
class LearnInstanceVar @a=1 def printVar() puts "a: #{@a}" end end test1 = LearnInstanceVar.new test1.printVar输出
$ ruby test.rb a:正确的定义
class LearnInstanceVar def initialize(a) @a=a end def printVar() puts "a: #{@a}" end end test1 = LearnInstanceVar.new("hello") test1.printVar输出
$ ruby test.rb a: hello类似java中的private,但是更严格,连定义的位置都只能放在特定的方法里面
类变量
类变量的打印
类变量的生存周期
- 类变量可以在多个实例之间公用,类似java的 static
- 在类的方法体以外声明
#!/usr/bin/ruby class Customer @@no_of_customers=0 def printCus() @@no_of_customers += 1 puts "Total number of customers : #{@@no_of_customers}" end end cust1=Customer.new cust2=Customer.new cust1.printCus() cust2.printCus()
全局变量
- 全局变量以$符号打头
- 全局变量可以在类与类之间共享
Ruby 运算符
比较运算符
== 和 equal?
- equal? 是比较两个对象是否是同一个对象
- == 是比较两个对象是否相等
a = "Ruby" # 定义一个字符串对象 b = "Ruby" # 虽然和a的内容相同,但是他们是不同的对象 a.equal?(b) # false: a和b指向不同的对象 a == b # true: 他们的内容是相同的
eq? 是 equal? 的缩写
<=> 联合比较运算符
=== 三等号
这个运算符更神奇:
通常情况下这中方式与==是一样的,但是在某些特定情况下,===有特殊的含义:
- 在Range中===用于判断等号右边的对象是否包含于等号左边的Range;
- 正则表达式中用于判断一个字符串是否匹配模式,
- Class定义===来判断一个对象是否为类的实例,
- Symbol定义===来判断等号两边的符号对象是否相同。
(1..10) === 5 # true: 5属于range 1..10 /\d+/ === "123" # true: 字符串匹配这个模式 String === "s" # true: "s" 是一个字符串类的实例 :s === "s" # true
.eql?
并行赋值
a = 10 b = 20 c = 30可以写成这样
a, b, c = 10, 20, 30于是在java和c中很麻烦的变量交换,在ruby中可以很简单的写成
a, b = b, c这样的代码
a=1 b=2 c=3 a,b=b,c puts "a: #{a}" puts "b: #{b}" puts "c: #{c}"执行结果为
$ ruby test.rb a: 2 b: 3 c: 3
范围运算符
- 1..10 创建了一个从1 到10的范围,并且包含10
- 1...10 跟上面那个唯一的不同是不包含10
define? 运算符
foo = 42 defined? foo # => "local-variable" defined? $_ # => "global-variable" defined? bar # => nil(未定义)还可以检测方法是否定义了
defined? method_call # 如果方法已经定义,则为 True
defined? puts # => "method" defined? puts(bar) # => nil(在这里 bar 未定义) defined? unpack # => nil(在这里未定义)
Ruby 点运算符 "." 和双冒号运算符 "::"
您只需要在表达式的常量名前加上 :: 前缀,即可返回适当的类或模块对象。
如果未使用前缀表达式,则默认使用主 Object 类。
MR_COUNT = 0 # 定义在主 Object 类上的常量 module Foo MR_COUNT = 0 ::MR_COUNT = 1 # 设置全局计数为 1 MR_COUNT = 2 # 设置局部计数为 2 end puts MR_COUNT # 这是全局常量 puts Foo::MR_COUNT # 这是 "Foo" 的局部常量
原文:http://blog.csdn.net/nsrainbow/article/details/49834097
内容总结
以上是互联网集市为您收集整理的有趣的Ruby-学习笔记1全部内容,希望文章能够帮你解决有趣的Ruby-学习笔记1所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。