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 ...
随机推荐
- C语言关键字static的绝妙用途
为什么要说static妙,它确实是妙,在软件开发或者单片机开发过程中,大家总以为static就是一个静态变量,在变量类型的前面加上就自动清0了,还有就是加上static关键字的,不管是变量还是关键字, ...
- 【Android 应用开发】BluetoothSocket详解
一. BluetoothSocket简介 1. 简介 客户端与服务端 : BluetoothSocket 和 BluetoothServerSocket 类似于Java中的套接字的 Socket 和 ...
- android的左右侧滑菜单实现
最近看了很多app应用都采用的是左右侧滑,比如网易新闻.凡客等 这里也试着写一下侧滑 首先看一下效果 然后给出xml布局代码 <RelativeLayout xmlns:android=&quo ...
- mybatis源码之PreparedStatementHandler
/** * @author Clinton Begin */ public class PreparedStatementHandler extends BaseStatementHandler { ...
- ZooKeeper 会话超时
1.会话概述 在ZooKeeper中,客户端和服务端建立连接后,会话随之建立,生成一个全局唯一的会话ID(Session ID).服务器和客户端之间维持的是一个长连接,在SESSION_TIMEOUT ...
- decode ways(动态规划)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- java基础语法(一)
java基础语法(一) 1.类是一种抽象的概念,对象是类的一种具体表示形式,是具体的概念.先有类,然后由类来生成 对象(Object).对象又叫做实例(Instance). 2.类由两大部分构成:属性 ...
- Day5_递归_二分法
递归调用: 在调用一个函数的过程中,直接或间接的调用函数本身. def func(): print('from func') 间接调用: def foo(): print('form foo') ba ...
- How Microservices are Transforming Python Development
https://blog.appdynamics.com/engineering/how-microservices-are-transforming-python-development/ Summ ...
- 14 Live CDs for Penetration Testing (Pen Test) and Forensic
http://www.ivizsecurity.com/blog/penetration-testing/live-cd-penetration-testing-pen/ Yesterday I wa ...