简体   繁体   English

当你说Ruby是反思的时候,这主要是指“鸭子打字”吗?

[英]When you say Ruby is reflective, does this mainly refer to “duck typing”?

I was reading a text describing Ruby and it said the following: 我正在阅读描述Ruby的文本,它说如下:

Ruby is considered a “reflective” language because it's possible for a Ruby program to analyze itself (in terms of its make-up), make adjustments to the way it works, and even overwrite its own code with other code. Ruby被认为是一种“反思”语言,因为Ruby程序可以分析自身(就其构成而言),调整其工作方式,甚至用其他代码覆盖自己的代码。

I'm confused by this term 'reflective' - is this mainly talking about the way Ruby can look at a variable and figure out whether it's an Integer or a String (duck typing), eg: 我对这个术语“反思”感到困惑 - 这主要是谈论Ruby可以查看变量的方式,并弄清楚它是一个整数还是一个字符串(鸭子打字),例如:

x = 3
x = "three" # Ruby reassigns x to a String type

To say Ruby is "reflective" means that you can, for instance, find out at runtime what methods a class has: 要说Ruby是“反思的”意味着你可以在运行时找出一个类有哪些方法:

>> Array.methods
=> ["inspect", "private_class_method", "const_missing",
[ ... and many more ... ]

(You can do the same thing with an object of the class.) (你可以用类的对象做同样的事情。)

Or you can find out what class a given object is... 或者你可以找出给定对象是什么类...

>> arr = Array.new
=> []
>> arr.class
=> Array

And find out what it is within the class hierarchy... 并找出它在类层次结构中的含义......

>> arr.kind_of?
>> arr.kind_of? Array
=> true
>> arr.kind_of? String
=> false

In the quote where they say "it's possible for a Ruby program to analyze itself" that's what they're talking about. 在引用中,他们说“Ruby程序可以分析自己”这就是他们所说的。

Other languages such as Java do that too, but with Ruby it's easier, more convenient, and more of an everyday part of using the language. 其他语言(如Java)也可以这样做,但使用Ruby它更容易,更方便,而且更多的是使用该语言的日常部分。 Hence, Ruby is "reflective." 因此,Ruby是“反思的”。

No, it means that you can issue a ruby command to get information about, well, just about anything. 不,这意味着您可以发出ruby命令来获取有关任何内容的信息。 For example, you can type the command File.methods() to get a listing of all methods belonging to the File module. 例如,您可以键入命令File.methods()以获取属于File模块的所有方法的列表。 You can do similar things with classes and objects -- listing methods, variables, etc. 你可以用类和对象做类似的事情 - 列出方法,变量等。

Class reopening is a good example of this. 课程重新开放就是一个很好的例子。 Here's a simple example: 这是一个简单的例子:

class Integer
    def moxy
        if self.zero?
            self - 2
        elsif self.nonzero?
            self + 2          
        end      
    end  
end

puts 10.moxy

By reopening a standard Ruby class - Integer - and defining a new method within it called 'moxy', we can perform a newly defined operation directly on a number. 通过重新打开标准的Ruby类 - Integer - 并在其中定义一个名为'moxy'的新方法,我们可以直接对一个数字执行新定义的操作。 In this case, I've defined this made up 'moxy' method to subtract 2 from the Integer if it's zero and add two if it's nonzero. 在这种情况下,我已经定义了这个组成'moxy'的方法,如果它为零则从Integer中减去2,如果它非零,则加2。 This makes the moxy method available to all objects of class Integer in Ruby. 这使得moxy方法可用于Ruby中的Integer类的所有对象。 (Here we use the 'self' keyword to get the content of the integer object). (这里我们使用'self'关键字来获取整数对象的内容)。

As you can see, it's a very powerful feature of Ruby. 如您所见,它是Ruby的一个非常强大的功能。

EDIT: Some commenters have questioned whether this is really reflection . 编辑:一些评论者质疑这是否真的反映 In the English language the word reflection refers to looking in on your own thoughts. 在英语中,“反思”一词指的是查看自己的想法。 And that's certainly an important aspect of reflection in programming also - using Ruby methods like is_a, kind_of, instance_of to perform runtime self-inspection. 这当然也是编程反思的一个重要方面 - 使用像is_a,kind_of,instance_of这样的Ruby方法来执行运行时自检。 But reflection also refers to the the ability of a program to modify its own behavior at runtime. 但反射也指程序在运行时修改自身行为的能力。 Reopening classes is one of the key examples of this. 重新开放课程是其中一个重要的例子。 It's also called monkey patching . 它也被称为猴子补丁 It's not without its risks but all I am doing is describing it here in the context of reflection, of which it is an example. 它并非没有风险,但我所做的只是在反思的背景下描述它,这是一个例子。

It refers mainly at how easy is to inspect and modify internal representations during run-time in Ruby programs, such as classes, constants, methods and so on. 它主要指的是在Ruby程序的运行时检查和修改内部表示是多么容易,例如类,常量,方法等。

Most modern languages offer some kind of reflective capabilities (even statically typed ones such as Java), but in Ruby, it is so easy and natural to use these capabilities, that it really make a real difference when you need them. 大多数现代语言都提供某种反射功能(甚至是静态类型的Java,如Java),但在Ruby中,使用这些功能非常容易和自然,它确实在您需要时真正发挥作用。

It just makes meta-programming, for example, an almost trivial task, which is not true at all in other languages, even dynamic ones. 它只是使元编程,例如,一项几乎无足轻重的任务,在其他语言中根本不是真的,甚至是动态的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以用鸭子打字改进这个方法吗? - Could I improve this method with duck typing? 如何在Ruby on Rails的模型类中引用模型的实例? - How do you refer an instance of a model in model class in Ruby on Rails? 为什么ruby-debug说'保存的帧可能不完整' - Why does ruby-debug say 'Saved frames may be incomplete' Ruby on Rails问题..主要处理红宝石/捆绑器 - Ruby on Rails issue..mainly dealing with ruby gems/bundler 在搜索栏中键入内容时,将显示Ruby on Rails先前的搜索 - Ruby on Rails Previous Searches Display when Typing in Search Bar 你怎么能在Ruby中从一个特定的字母开始对数组进行排序,比如字母f? - How can you sort an array in Ruby starting at a specific letter, say letter f? Ruby / Rails需要比PHP应用程序更多的单元测试吗? - Does Ruby/Rails require more unit testing than say a PHP app? Ruby on Rails 什么是“说”方法? - Ruby on Rails what is "say" method? 假设您在轨道上扩展了红宝石。 有没有一种好的方法可以动态使用该代码,而不必重新安装所有的gem? - Lets say you expand ruby on rails. Is there a good way to dynamically use that code, without having to reinstall all of the gems? 为什么Ruby on Rails书或引用总是说更新是由PUT而Destroy是由DELETE而不是? - Why Ruby on Rails books or references always say Update is by PUT and Destroy is by DELETE when it is not?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM