在Ruby模块中的私有类(不是类方法)? [英] Private class (not class method) in a Ruby module?

查看:189
本文介绍了在Ruby模块中的私有类(不是类方法)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Ruby的新手(有Python,C ++和C的经验)。我需要创建一个只有被模块中的其他类和方法使用的类。在Python中,我将其称为__classname。我会在C ++中使用一个空的typedef。如何在Ruby中做这个(或者我是在错误的树上,而不是这样做的Ruby方式?)

I'm new to Ruby (experienced with Python, C++ and C). I need to create a class that is only to be used by other classes and methods in a module. In Python, I'd just call it __classname. I'd use an empty typedef in C++. How do I do this in Ruby (or am I barking up the wrong tree and not doing this the "Ruby way"?)

推荐答案

p>我还没有看到这样的概念到目前为止在Ruby,但我想你可以模拟,通过创建私有方法,将返回一个类创建为一个局部变量(记住在Ruby中,一个类是一个对象,就像任何其他,并且可以在方法中实例化并由它返回)。

I haven't seen such concept so far in Ruby, but I guess you could simulate that by creating private method which would return a class created as a local variable (remember that in Ruby, a class is an object just like any other, and can be instantiated in a method and returned by it).

BTW,即使Ruby中的私有方法不像其他语言中的私有方法 - 他们使用 send 方法。但这样做意味着你知道你在做什么。

BTW, even private methods in Ruby aren't as private as in other languages - you can always access them using send method. But doing that implies you know what you are doing.

这篇关于在Ruby模块中的私有类(不是类方法)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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