Ruby-扩展Ruby中的内置变量 [英] Ruby - extend built-in variables in Ruby

查看:105
本文介绍了Ruby-扩展Ruby中的内置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展Ruby中变量的功能.原因是我正在从事类似类型系统或值检查器的工作(这听起来有点疯狂,但是整个想法很长一段时间都无法解释,这只是我想扩展默认变量的原因).

I would like to extend the functionality of variables in Ruby. The reason is I am working on something similar of a type system or value checker (this sounds a bit crazy but the whole idea is to long to explain, just the reason I would like to extend default variables).

我已经读过在Ruby中,一切都是对象.所以变量是对象.而且Ruby对于通过元编程可以更改的内容相当宽容.

I have read that in Ruby, everything is an object; so variables are objects. And Ruby is supposed to be quite liberal concerning what can be changed via meta-programming.

是否存在与我可以扩展的局部变量相关的某种类"?

Is there some kind of 'Class' associated with local variables that I could extend?

我想为每个包含一个类型的字符串表示形式的变量关联一个字符串变量.此外,我想拦截变量分配并在每次将新值分配给变量时执行一个方法.这样一来,我就可以根据类型(以字符串形式存储在变量中)检查新值是否正确.

I would like to associate a string-variable for each variable that holds a string representation of a type. Further I would like to intercept variable assignments and execute a method each time a new value is assigned to a variable. This is so I can check, if the new value is correct according to the type (stored as string in the Variable).

如果将Ruby中的局部变量定义为类的对象,则可以扩展该类或通过ruby mixin对其进行修改.

If local variables in Ruby are defined as object of a class, I can extend that class or modify it via a ruby mixin.

解决方法是为我的变量创建一个新类(而不使用Ruby的局部变量中的构建).此类可以具有值属性,类型的属性(存储为字符串)以及获取方法和设置方法.这样可以解决我的问题,但是如果可能的话,我想用ruby扩展内置变量.

The workaround would be to create a new class for my Variables (and not use the build in local variables of Ruby). This class can have a value attribute, a attribute (stored as string) for the type and a get- and set-method. This way I can solve my problem, but I would like to extend the built in variables in ruby, if that is possible.

当前正在进行的工作

class Fixnum
        attr_accessor :tp
        @tp

        def mytype ( type )
                @tp = type
        end
        def typecheck
                #call typechecker
                puts "checked"
        end
end

测试代码:

a = 3
a.mytype("nat")
puts a.tp
a.typecheck

仍然存在两个问题. 首先,我认为无法向Fixnum添加新的构造函数. 第二,我想拦截变量访问,即"b = a"调用a的方法"typecheck".但这需要类似于面向方面的编程的东西,我不确定是否可以使用Ruby的元编程便利来解决.

There are still two problems. First, I think it is not possible to add a new constructor to Fixnum. Second, I would like to intercept the variable access, i.e. "b = a" calls the method 'typecheck' of a. But this would require something similar to Aspect Oriented Programming and I am not sure if this can be solved with Ruby's meta-programming facilitates.

推荐答案

我已经读过在Ruby中,一切都是对象

I have read that in Ruby, everything is an object

这取决于您对"对象"和每一个"事物"的定义. " Object "可以表示"可由程序操纵的实体"(从现在开始,我将其称为 object ),或"是对象系统成员的值"(从现在开始,我将其称为Object).

That depends on your definition of "object" and every-"thing". "Object" can mean "entity that can be manipulated by the program" (which I will call object from now on), or "value that is a member of the object system" (which I will call Object from now on).

在Ruby中,程序可以操纵的所有内容(即每个对象)也是Object,即类的实例.例如,这与Java不同,在Java中,基元可以由程序操纵(即,在这个词的意义上是 objects ),但不是Objects.在Ruby中,不存在这种区别:每个对象是一个Object,每个Object也是一个对象.

In Ruby, everything that can be manipulated by the program (i.e. every object) is also an Object, i.e. an instance of a class. This is unlike Java, for example, where primitives can be manipulated by the program (i.e. are objects in that sense of the word), but aren't Objects. In Ruby, this distinction doesn't exist: every object is an Object and every Object is also an object.

但是,该语言中有事物,这些 不能被程序操纵,也不是类的实例,即它们都不是对象Object.例如,这些是方法,变量,语法,参数列表,参数列表,关键字.

However, there are things in the language, which cannot be manipulated by the program and which aren't instances of a class, i.e. they are neither object s nor Objects. These are, for example, methods, variables, syntax, parameter lists, arguments lists, keywords.

注意:您可以使用Ruby的反射API为您提供一个表示方法或参数列表的对象,但是该对象只是一个代理,不是真实的东西.

Note: you can use Ruby's reflection API to give you an object that represents a method or a parameter list, but that object is only a proxy, it is not the real thing.

所以,当我们说"一切都是对象"时,我们真正的意思是每个对象都是Object",即可以被程序操纵的对象也是对象系统的成员,换句话说,对象系统之外没有值(与Java中的原语不同). 不是并不意味着该语言中存在的所有内容都可以在运行时由程序进行操纵.

So, when we say "everything is an object", what we really mean is that "every object is an Object", i.e. that everything which can be manipulated by the program is also a member of the object system, or in other words, there are no values outside of the object system (unlike primitives in Java). We do not mean that everything that exists in the language can also be manipulated at runtime by the program.

所以变量就是对象

so variables are objects

不幸的是,它们既不是 object 也不是Object s.

No, unfortunately, they are neither object s nor Objects.

这在Ruby语言规范中也有明确说明(我加了强调):

This is also clearly stated in the Ruby Language Specification (emphasis added by me):

6.2变量

6.2.1概述

变量用名称表示,指代对象,称为变量的值. 变量本身不是对象.

6.2 Variables

6.2.1 General description

A variable is denoted by a name, and refers to an object, which is called the value of the variable. A variable itself is not an object.

在Matz和David Flanagan的 The Ruby Programming Language 中>在第2页上说:

In the book The Ruby Programming Language by Matz and David Flanagan it says on page 2:

每个值都是一个对象

every value is an object

请注意,它不是说所有-事情,而是说每个.

Note, it doesn't say every-thing, only every value.

另请参阅问题变量是红宝石中的对象吗?

这篇关于Ruby-扩展Ruby中的内置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆