[Ruby笔记]11.Ruby == .equal? object .object_id .respond_to? .send()
来源:互联网 发布:2017淘宝双十一攻略 编辑:程序博客网 时间:2024/05/18 23:57
Ruby object .object_id .respond_to? .send()
obj.rb source code
.object_id
可以获取对象id
,该id
唯一确认一个对象;request = gets.chomp
从命令行得到输入,获取想要执行的方法;.respond_to?("xxx")
查找对象是不是有方法xxx
;.send(xxx)
如果对象obj
有xxx
方法,那么obj.send(xxx)
相当于obj.xxx
执行;
PS C:\Users\Administrator\RubyCode> more obj.rbobj = Object.newdef obj.hello "hello world"enddef obj.year "2016/05/23"endputs "obj's id is #{obj.object_id}"puts "Information : "request = gets.chompif obj.respond_to?(request) puts obj.send(request)else puts "No such inforamtion avaiable."end
run the code
PS C:\Users\Administrator\RubyCode> ruby obj.rbobj's id is 21802380Information :PS C:\Users\Administrator\RubyCode> ruby obj.rbobj's id is 22326540Information :hellohello worldPS C:\Users\Administrator\RubyCode> ruby obj.rbobj's id is 22334800Information :year2016/05/23PS C:\Users\Administrator\RubyCode> ruby obj.rbobj's id is 22236380Information :hiNo such inforamtion avaiable.
object equal ==
- 创建两个变量
hello1
以及hello2
,赋同样的值“hello world”
:
PS C:\Users\Administrator\RubyCode> irb --simple-prompt>> hello1 = "hello world"=> "hello world">> hello2 = "hello world"=> "hello world"
- 使用
.object_id
查看object id
:
>> hello1.object_id=> 22097940>> hello2.object_id=> 25810140
- 使用
==
毕竟是做值的判断,输出了true
; - 使用
a.equal? b
判断是否为同一个对象,输出false
;
>> hello1 == hello2=> true>> hello1.equal? hello2=> false
reference
《The Well-Grounded Rubyist, Second Edition》
(https://www.manning.com/books/the-well-grounded-rubyist-second-edition)
2.3. The innate behaviors of an object
∧_∧ / (゚д゚ )/マイアヒ~♪ |⊃ ⊃\♪マイアフ~♪ | | \ ∪-∪http://emoji.vis.ne.jp/myahe.htm
0 0
- [Ruby笔记]11.Ruby == .equal? object .object_id .respond_to? .send()
- Ruby中 respond_to? 和 send 的用法
- Ruby中 respond_to? 和 send 的用法
- Ruby中 respond_to? 和 send 的用法
- Ruby中 respond_to? 和 send 的用法
- ruby中的respond_to和send的用法
- Ruby 之 respond_to? 和 send 的用法
- ruby respond_to? 和 send的用法
- Ruby中 respond_to? 和 send 的用法
- Ruby中 respond_to? 和 send 的用法 .
- Ruby vs Java -- "== 与 equal"
- Ruby on Rails 入门 ----respond_to
- Ruby on Rails 入门 ----respond_to
- ruby send mail
- [Ruby笔记]10. Ruby object return Boolean nil false #{}
- [Ruby笔记]13.Ruby object .replace("") .dup .freeze
- [Ruby]ruby笔记01
- [Ruby]ruby笔记02
- 地铁译:Spark for python developers ---构建Spark批处理和流处理应用前的数据准备
- [Ruby笔记]8. Ruby Rakefile rake 删除文件 确认
- 【Java】Java内存参数配置
- [Ruby笔记]10. Ruby object return Boolean nil false #{}
- 身份证的奥秘问题(待修改)
- [Ruby笔记]11.Ruby == .equal? object .object_id .respond_to? .send()
- FPGA:下一代机器人感知处理器
- AT5 two-dimensional objects - the torus and genus
- cobar初识记录(mysql)
- [Ruby笔记]9.Ruby文档工具 ri ruby-doc Windows CMD.exe Powershell
- 哈夫曼树及其应用
- java学习总结(16.05.17)对数据类型取值范围和数据溢出的理解(以byte类型为例)
- NuGet命令的用法
- Android Studio查看类继承体系和类中的方法的快捷键