Ruby类
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" 的局部常量
Ruby类的更多相关文章
- 雷林鹏分享:Ruby 类和对象
Ruby 类和对象 Ruby 是一种完美的面向对象编程语言.面向对象编程语言的特性包括: 数据封装 数据抽象 多态性 继承 这些特性将在 面向对象的 Ruby 中进行讨论. 一个面向对象的程序,涉及到 ...
- 雷林鹏分享:Ruby 类案例
Ruby 类案例 下面将创建一个名为 Customer 的 Ruby 类,您将声明两个方法: display_details:该方法用于显示客户的详细信息. total_no_of_customers ...
- Ruby 类和对象
Ruby 类和对象 Ruby 是一种完美的面向对象编程语言.面向对象编程语言的特性包括: 数据封装 数据抽象 多态性 继承 这些特性将在 面向对象的 Ruby 中进行讨论. 一个面向对象的程序,涉及到 ...
- Ruby类的继承
Ruby继承的语法 class DerivedClass < BaseClass #some stuff end < 为继承符号 重写(override) 的概念 有时, 我们希望子类从父 ...
- Ruby类的创建与使用
Ruby是一种面向对象编程语言,这意味着它操纵的编程结构称为"对象" 先上代码, 了解类的定义与使用方式 class Computer $manufacturer = " ...
- Ruby类,模块1
类的扩展和继承 class Fixnum def dosome(str) puts str end def abs puts "覆盖了原有的方法" end end puts 1.c ...
- ruby 类创建-继承-消息
############################################# #create ruby a class #@符号表示实例变量,相当于java的private 属性 ### ...
- Ruby类扩张(extension)
创建: 2017/09/07 更新: 2017/09/16 修改标题字母大小写 ruby ---> Ruby 扩张类 class 类名 扩张的内容 end ...
- Ruby 类案例
#!/user/bin/ruby # -*-coding:UTF-8-*- class Customer @@no_of_customers=0 def initialize(id,name,addr ...
随机推荐
- (NO.00001)iOS游戏SpeedBoy Lite成形记(三)
在Xcode中建立新类Player,继承自CCSprite.因为我们之后需要方便的更换玩家的大头贴,所以需要能够以不同的大头贴参数初始化Player对象. 不过别急,想想我们还需要在Player对象初 ...
- SpriteBuilder中粒子发射器的reset on visibility toggle选项解释
如果选中该选择框,表示粒子发射器将删除所有已存在的粒子当它们的可见状态被代码改变的时候. 如果该选择框没有选中,则发射器将保持产生粒子但不渲染它们(意思是有但你看不到)当它们的可视状态为NO的时候. ...
- Android 数据库框架ormlite
Android 数据库框架ormlite 使用精要 前言 本篇博客记录一下笔者在实际开发中使用到的一个数据库框架,这个可以让我们快速实现数据库操作,避免频繁手写sql,提高我们的开发效率,减少出错的机 ...
- 使用HTML5抓取 Audio & Video
原文地址: http://www.html5rocks.com/en/tutorials/getusermedia/intro/ 本地化的文章: http://www.html5rocks.com/z ...
- Make3D Convert your image into 3d model
Compiling and Running Make3D on your own computer source: http://make3d.cs.cornell.edu/code_linux.ht ...
- 浅析数据结构中栈与C实现
最近在搞摄像头驱动,o()︿︶)o 唉,别提有多烦,一堆寄存器就有人受的了--特么这不是单片机的开发,这是内核驱动开发-- 今天放松一下,我们来看看数据结构中的栈,这节的知识点可以说是数据结构中最容易 ...
- Android实训案例(六)——四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听
Android实训案例(六)--四大组件之一BroadcastReceiver的基本使用,拨号,短信,SD卡,开机,应用安装卸载监听 Android中四大组件的使用时重中之重,我这个阶段也不奢望能把他 ...
- OAF更改动态头行
选择头信息,动态刷新行信息.本文将详细介绍该种需求的做法. 本例沿用<OAF-头行结构>的am与vo,所以在进行本例之前,请先完成<OAF-头行结构> 一.创建页面 在test ...
- AngularJS进阶(六)AngularJS+BootStrap实现弹出对话框
AngularJS+BootStrap实现弹出对话框 参考资料: http://angular-ui.github.io/bootstrap/#/modal https://www.zybuluo.c ...
- zookeeper 应用开发
由于zookeeper的client只有zookeeper一个对象,使用也比较简单,所以就不许要文字说明了,在代码中注释下就ok 了. 1.测试用的main方法 package ClientExamp ...