`
Hooopo
  • 浏览: 335382 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Top Level Environment and Object main in Ruby

    博客分类:
  • Ruby
阅读更多

本文只是将有关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

  

分享到:
评论
1 楼 Hooopo 2009-04-21  
还有就是top level 常量的问题
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"


相关推荐

    3D-RCNN: Instance-level 3D Object Reconstruction via Render-and-Compare

    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

    "toplevel.as"很可能是一个包含主函数`main()`或`applicationMain()`的类,这个函数在程序启动时被调用,负责初始化整个应用程序。 错误提示“找不到toplevel.as”可能由以下几个原因引起: 1. **文件路径问题**:...

    toplevel.as文件_flash_cs4

    toplevel.as文件.rar flash cs4编译flash cs3或更低版本时需要的文件 放在class下面  解决方法:  1、打开Flash CS4,选择编辑 &gt; 首选参数 &gt; ActionScript项 &gt; ActionScript 2.0 设置 &gt; 点击”+”  2、添加新...

    QUARTUS II 编译报错top level design entity “...” is undefined

    在使用Altera公司的集成开发环境QUARTUS II进行FPGA设计时,你可能会遇到一个常见的错误:“top level design entity “….” is undefined”。这个错误通常表示QUARTUS II无法识别你的顶层设计实体,也就是说,它找...

    FLash CS4文件toplevel.as

    无法找到对动作脚本 2.0 进行类型检查所需的文件“toplevel.as”。请确保目录“$(LocalData)/Classes”在动作脚本首选参数的全局类路径中列出。  解决方法:  1、打开Flash CS4,选择编辑 &gt; 首选参数 &gt; ...

    英文原版-PHP Advanced and ObjectOriented Programming 3rd Edition

    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 ...

    PHP Advanced and Object-Oriented Programming, 3rd Edition 英文原版

    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

    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中需要用到的toplevel.cs文件

    在使用Flash CS4的时候,按Ctrl+Enter预览时,常常会输出错误信息,“无法找到对动作脚本 2.0 进行类型检查所需的文件“toplevel.as”。请确保目录“$(LocalData)/Classes”在动作脚本首选参数的全局类路径中列出...

    Preproduction.Blueprint.How.to.Plan.Game.Environments.and.Level.Designs.2nd.Ed

    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 and Dynamic Implicit Surfaces

    Level Set Methods由Stanley Osher和Ronald Fedkiw等人提出,并在Osher和Fedkiw的书《Level Set Methods and Dynamic Implicit Surfaces》中有详细论述,这本书被认为是论述Level Set Methods最完整的书籍之一,尤其...

    Real-Time C++: Efficient Object-Oriented and Template Microcontroller

    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. ...

    Object Tracking A Survey

    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 ...

    Ruby.Pocket.Reference

    - 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** ...

    Concurrency in Main-Memory Database Systems

    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 ...

    Flash CS4:无法找到对动作脚本2.0 进行类型检查所需的文件toplevel.as问题

    NULL 博文链接:https://huanyq2008.iteye.com/blog/756888

Global site tag (gtag.js) - Google Analytics