- 浏览: 335382 次
- 性别:
- 来自: 北京
文章分类
最新评论
本文只是将有关ruby top level environment 和main Object的sample进行简单比较,仔细看完比较的结果后您自然会得结论。
运行环境是在文件中,与irb运行环境在某些地方会有出入,请参照http://www.iteye.com/topic/125046
1.top level environment 中默认receiver是一个叫main的Object
p self#main p self.class#Object
2.默认情况,在top level environment定义的方法称为main的私有方法,在irb中貌似默认是public method。
def m end p self.singleton_methods(false).include? "m" #false p Object.singleton_methods(false).include? "m" #false p self.methods(false).include? "m" #false p self.public_methods(false).include? "m" #false p self.private_methods(false).include? "m"#true p Object.instance_methods(false).include? "m" #false p Kernel.private_methods(false).include? "m" #false
3.top level environment可以和像在普通类中一样用public声明方法定义,并且被public声明后称为所有Object实例的public method
public def m end p self.singleton_methods(false).include? "m" #false p Object.singleton_methods(false).include? "m" #false p self.methods(false).include? "m" #false p self.public_methods(false).include? "m" #true p self.private_methods(false).include? "m"#false p Object.instance_methods(false).include? "m" #true p Kernel.private_methods(false).include? "m" #false
4.和在一般class中一样,局部变量不能被top level方法调用。
var="hooopo" def get_var p var end get_var #in `get_var': undefined local variable or method `var' for main:Object (NameError)
5.和在一般class中一样,top level定义的实例变量可以被top level中的方法调用。
@var="hooopo" def get_var p @var end get_var #"hooopo"
6.在top level定义的实例变量属于默认receiver main Object,并且不属于任何非main Object的Object。
@var="hooopo" obj=Object.new p obj #=>#<Object:0x2a62f64> p self #=>#<Object:0x494f9e0 @var="hooopo">
7.top level中定义的类变量成为Object的类变量,由于类变量可以被继承到子类,并且被子类改变,才有了慎用类变量 - 实例变量靠谱量又足
中“类变量(@@xx)的问题在于它本质跟全局变量一样”的说法。。
@@var="hooopo" def Object.get_var_in_obj p @@var end class String def String.get_var_in_str class_variable_get("@@var") end def String.set_var_in_str class_variable_set("@@var","hooopo is changed") end end Object.get_var_in_obj #"hooopo" p Object.class_variables #["@@var"] p String.class_variables #["@@var"] p String.get_var_in_str #"hooopo" String.set_var_in_str p @@var #"hooopo is changed"
8.全局变量是名副其实的全局变量,无论是在top level还是class中定义,都可以被获取。
$var="hooopo" def get_var p $var end get_var #"hooopo" class C $var_c="Hooopo" def get_var p $var_c end end p $var_c #"hooopo" c=C.new #"hooopo" c.get_var #"Hooopo" p $var_c #"Hooopo"
9.可以为main Object定义单体方法...
class << self def m p "singleton method of top-level Object instance" end end m#"singleton method of top-level Object instance" self.m #"singleton method of top-level Object instance" o=Object.new o.m # undefined method `m' for #<Object:0x2a62dfc> (NoMethodError)
10.main Object的单体方法调用优先于instance method。
def m p "instance method of top level Object instance" end class << self def m p "singleton method of top-level Object instance" end end m#"singleton method of top-level Object instance" self.m #"singleton method of top-level Object instance"
11.top level环境更像是在一个类中...
def m p "instance method of top level Object instance" end #class << self # def m # p "singleton method of top-level Object instance" # end #end m#"instance method of top level Object instance" self.m #private method `m' called for main:Object (NoMethodError)
12.public声明,让top level方法真正top level
public def m p "instance method of top level Object instance" end #class << self # def m # p "singleton method of top-level Object instance" # end #end m#"instance method of top level Object instance" self.m #"instance method of top level Object instance"
13.在类中可以调用top level method
def top_level_method p "hooopo" end class C def instance_method_of_c top_level_method end end c=C.new c.instance_method_of_c #"hooopo"
13.同11
def top_level_method p "hooopo" end class C def instance_method_of_c self.top_level_method end end c=C.new c.instance_method_of_c #`instance_method_of_c': private method `top_level_method' called for #<C:0x2a62d34> (NoMethodError)
14.优先级问题,类中实例方法优先于top level 方法
public
def top_level_method
p "hooopo" end class C def top_level_method p "hooopo-in instance method of C" end def instance_method_of_c top_level_method end end c=C.new c.instance_method_of_c #"hooopo-in instance method of C"
15.优先级问题,类中实例方法优先于top level 方法,也优先于Kernel模块中方法
def puts STDOUT.puts "from top level" end class C def puts STDOUT.puts "from insctance method of C" end def self.puts STDOUT.puts "from sington method of C" end def call_puts puts end end c=C.new c.call_puts #from insctance method of C
16.top level中方法优先于Kernel模块中的方法。
def puts STDOUT.puts "from top level" end class C #def puts # STDOUT.puts "from insctance method of C" #end def self.puts STDOUT.puts "from sington method of C" end def call_puts puts end end c=C.new c.call_puts #from top level
17.同16
#def puts # STDOUT.puts "from top level" #end class C #def puts # STDOUT.puts "from insctance method of C" #end def self.puts STDOUT.puts "from sington method of C" end def call_puts puts end end c=C.new c.call_puts #"\n"=>from Kernel?
18. 关于main Object 实现的猜测:
class Object Object.new.instance_eval do def self.to_s "main" end private ## # Your program gets inserted here... ## end end
评论
irb(main):001:0> CONST="HOOOPO" => "HOOOPO" irb(main):002:0> class T irb(main):003:1> CONST="hooopo" irb(main):004:1> def get_top_level_const irb(main):005:2> ::CONST irb(main):006:2> end irb(main):007:1> def get_class_const irb(main):008:2> CONST irb(main):009:2> end irb(main):010:1> end => nil irb(main):011:0> t=T.new => #<T:0x496e200> irb(main):012:0> t.get_top_level_const => "HOOOPO" irb(main):013:0> t.get_class_const => "hooopo" irb(main):014:0> ::CONST => "HOOOPO"
发表评论
-
新博客
2012-04-23 20:47 1735https://db-china.org -
Ruby Verbose Warning Mode
2011-10-16 14:48 2052Ruby在很多方面是一个更优雅的Perl,从Perl社区继承了 ... -
Pattern Match In Ruby
2011-10-07 01:17 2007最近看了一些Erlang,模式匹配是个好东西,简单的sum函数 ... -
Draper: View Models for Rails
2011-10-07 01:19 2270Draper是一个Ruby gem,它让Rails model ... -
Active Record batch processing in parallel processes
2011-10-07 01:20 2271Active Record 提供 find_each来分批处理 ... -
最轻量级的Ruby后台任务
2011-08-04 16:47 3861普通情况下ruby调用系统命令行的过程是堵塞的,无论是用sys ... -
test
2011-07-15 19:59 0test -
fiber
2011-06-17 09:37 0挖坑,待填。。 1.用到fiber.alive?、fiber ... -
Identity Map in Rails3.1
2011-06-12 18:29 2738Identity Map是Rails3.1的又 ... -
xx00
2011-06-06 03:40 0https://github.com/ngmoco/cache ... -
挖坑1
2011-06-06 02:17 0cache money 源码 替换memcache为redis ... -
websocket demo
2011-06-04 20:44 2055地址:https://github.com/hooopo/we ... -
ruby GC
2011-06-02 04:24 0http://blog.csdn.net/lijun84/a ... -
reduce method missing call stack with dynamic define method
2011-04-22 22:54 1594method_missing是ruby里面一个非常cool的h ... -
Autocompete with Trie
2011-04-09 04:04 1675像微薄里面用户输入一 ... -
用imagemagick和tesseract-ocr破解简单验证码
2011-04-09 01:31 18929工具:imagemagick + tesseract-ocr ... -
OAuth gem for rails,支持豆瓣,新浪微薄,腾讯微博,搜狐微博,网易微博
2011-03-26 03:13 4481地址:https://github.com/hooopo/oa ... -
用jmeter模拟amf请求进行压力测试
2010-12-16 16:56 30231.获取amf二进制包: 在本地建立proxy,端口为888 ... -
Memoization in Ruby
2010-11-14 11:42 1211这里的Memoization就是将ruby的方法或lambda ... -
整理了一下2008-2010的RubyHeroes博客列表
2010-10-07 02:26 2829Bryan Helmkamp(webrat作者)https:/ ...
相关推荐
the full 3D shape and pose of all object instances in the image. Our method produces a compact 3D representation of the scene, which can be readily used for applications like autonomous driving. Many ...
"toplevel.as"很可能是一个包含主函数`main()`或`applicationMain()`的类,这个函数在程序启动时被调用,负责初始化整个应用程序。 错误提示“找不到toplevel.as”可能由以下几个原因引起: 1. **文件路径问题**:...
toplevel.as文件.rar flash cs4编译flash cs3或更低版本时需要的文件 放在class下面 解决方法: 1、打开Flash CS4,选择编辑 > 首选参数 > ActionScript项 > ActionScript 2.0 设置 > 点击”+” 2、添加新...
在使用Altera公司的集成开发环境QUARTUS II进行FPGA设计时,你可能会遇到一个常见的错误:“top level design entity “….” is undefined”。这个错误通常表示QUARTUS II无法识别你的顶层设计实体,也就是说,它找...
无法找到对动作脚本 2.0 进行类型检查所需的文件“toplevel.as”。请确保目录“$(LocalData)/Classes”在动作脚本首选参数的全局类路径中列出。 解决方法: 1、打开Flash CS4,选择编辑 > 首选参数 > ...
Readers can take their PHP skills to the next level with this fully revised and updated PHP Advanced: Visual QuickPro Guide, Third Edition! Filled with fourteen chapters of step-by-step content and ...
Readers can take their PHP skills to the next level with this fully revised and updated PHP Advanced: Visual QuickPro Guide, Third Edition! Filled with fourteen chapters of step-by-step content and ...
Level set methods and fast marching methods: evolving interfaces in computational geometry, fluid mechanics, computer vision, and materials science[M]. Cambridge university press, 1999.
在使用Flash CS4的时候,按Ctrl+Enter预览时,常常会输出错误信息,“无法找到对动作脚本 2.0 进行类型检查所需的文件“toplevel.as”。请确保目录“$(LocalData)/Classes”在动作脚本首选参数的全局类路径中列出...
This could be for a playable level or a game environment exploration to show off in a portfolio. Planning process is called pre-production and what you end up with is a "Preproduction Blueprint". It...
Level Set Methods由Stanley Osher和Ronald Fedkiw等人提出,并在Osher和Fedkiw的书《Level Set Methods and Dynamic Implicit Surfaces》中有详细论述,这本书被认为是论述Level Set Methods最完整的书籍之一,尤其...
The appendices include a brief C++ language tutorial, information on the real-time C++ development environment and instructions for building GNU GCC cross-compilers and a microcontroller circuit. ...
Tracking is usually performed in the context of higher-level applications that require the location and/or shape of the object in every frame. Typically, assumptions are made to constrain the ...
- Describes global constants, which are defined at the top level of a program and are prefixed with `$`. They are used to store values that should not change throughout the program. 12. **Ranges** ...
and suggest solutions which are ideally suited to the changed environment. In the first part of this thesis, the optimal mechanism for creating snapshot in main-memory database systems is determined ...
NULL 博文链接:https://huanyq2008.iteye.com/blog/756888