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

允我心安 提交于 2019-12-04 11:31:13

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. 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. This makes the moxy method available to all objects of class Integer in Ruby. (Here we use the 'self' keyword to get the content of the integer object).

As you can see, it's a very powerful feature of 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. 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.

To say Ruby is "reflective" means that you can, for instance, find out at runtime what methods a class has:

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

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. Hence, Ruby is "reflective."

No, it means that you can issue a ruby command to get information about, well, just about anything. For example, you can type the command File.methods() to get a listing of all methods belonging to the File module. You can do similar things with classes and objects -- listing methods, variables, etc.

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.

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.

It just makes meta-programming, for example, an almost trivial task, which is not true at all in other languages, even dynamic ones.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!