修改 - 更改问题以澄清我的需求并消除任何混淆
我有一个带有类方法的超类,需要在每个子类中调用:
class SuperClass
def self.super_method
puts "super method was called"
end
end
我需要在每个子类的末尾调用此方法(在定义所有方法之后):
class SubClassA < SuperClass
def some_method
puts "some method was called"
end
super_method
end
class SubClassB < SuperClass
def some_other_method
puts "some method was called"
end
super_method
end
但是,我不想在每个子类的末尾手动调用super_method,就像上面的例子一样。相反,我想找到一些方法在超类的每个子类的末尾自动调用这个函数,这与继承的方法类似。
class SuperClass
def self.super_method
puts "super method was called"
end
self.inherited(subclass)
subclass.super_method
end
end
继承的原因不会起作用,因为只要子类继承自超类(在类&#39;类SubClassA&lt; SuperClass&#34;)上就会触发继承,但我需要方法是在定义了子类方法(some_method,在这种情况下为some_other_method)之后触发。我希望有一种类似于继承方法的构建方法可以做到这一点。
class SuperClass
def self.super_method
puts "super method was called"
end
self.methods_loaded(subclass) #not a real built in method, but hopefully something like this exists
subclass.super_method
end
end
如果ruby中没有内置这样的方法,那么我该如何模拟这个功能?
旧问题
注意 - 我要留下旧问题的一部分。如果您知道swift协议/委托模式,我认为下一部分将有助于明确我需要的内容和原因。如果你不愿意忽略这个问题。
我正在使用ruby创建自定义类,以便在Swift中添加类似于协议/委托模式的模式。我已经完成了很多工作,并且按照以下代码执行我需要的所有内容:
class SampleProtocol < Protocol
required_method :number_of_sections_in
required_method :number_of_rows_in_section
end
class SampleClass < SwiftClass
inherit_protocols :SampleProtocol
def number_of_sections_in; end
verify_conforms_to_protocols
end
我不太了解它是如何工作的,因为这不是现在的问题,但基本上SwiftClass包含上面的两个方法调用; inherit_protocols和verify_conforms_to_protocols。如果我要运行上面的代码,它会引发一个自定义的Method Missing错误,因为Sample Class不包含所需的方法:SampleProtocol中定义的number_of_rows_in_section。在verify_conforms_to_protocols方法中引发了错误。
基本上,所有这些逻辑都有效。我现在遇到的问题是,我不想在每个继承协议的类的末尾调用verify_conforms_to_protocols。相反,我正在寻找一种让SwiftClass的每个子类调用该方法之后>>子类的所有其他内部设置代码运行的方法(即:必须先调用它)方法已定义,否则类将永远不符合协议。)
我怎样才能做到这一点?
答案 0 :(得分:0)
我不知道你怎么能做你想做的事情而不诉诸于一个丑陋的棍棒的东西,如下所示。
/B Abort if an error is encountered. By default, CIPHER continues
executing even if errors are encountered.
/C Displays information on the encrypted file.
/D Decrypts the specified files or directories.
/E Encrypts the specified files or directories. Directories will be
marked so that files added afterward will be encrypted. The
encrypted file could become decrypted when it is modified if the
parent directory is not encrypted. It is recommended that you
encrypt the file and the parent directory.
/H Displays files with the hidden or system attributes. These files
are omitted by default.
/K Creates a new certificate and key for use with EFS. If this
option is chosen, all the other options will be ignored.
Note: By default, /K creates a certificate and key that conform
to current group policy. If ECC is specified, a self-signed
certificate will be created with the supplied key size.
/N This option only works with /U. This will prevent keys being
updated. This is used to find all the encrypted files on the
local drives.
/R Generates an EFS recovery key and certificate, then writes them
to a .PFX file (containing certificate and private key) and a
.CER file (containing only the certificate). An administrator may
add the contents of the .CER to the EFS recovery policy to create
the recovery key for users, and import the .PFX to recover
individual files. If SMARTCARD is specified, then writes the
recovery key and certificate to a smart card. A .CER file is
generated (containing only the certificate). No .PFX file is
generated.
Note: By default, /R creates an 2048-bit RSA recovery key and
certificate. If ECC is specified, it must be followed by a
key size of 256, 384, or 521.
/S Performs the specified operation on the given directory and all
files and subdirectories within it.
/U Tries to touch all the encrypted files on local drives. This will
update user's file encryption key or recovery keys to the current
ones if they are changed. This option does not work with other
options except /N.
/W Removes data from available unused disk space on the entire
volume. If this option is chosen, all other options are ignored.
The directory specified can be anywhere in a local volume. If it
is a mount point or points to a directory in another volume, the
data on that volume will be removed.
/X Backup EFS certificate and keys into file filename. If efsfile is
provided, the current user's certificate(s) used to encrypt the
file will be backed up. Otherwise, the user's current EFS
certificate and keys will be backed up.
/Y Displays your current EFS certificate thumbprint on the local PC.
/ADDUSER Adds a user to the specified encrypted file(s). If CERTHASH is
provided, cipher will search for a certificate with this SHA1
hash. If CERTFILE is provided, cipher will extract the
certificate from the file. If USER is provided, cipher will
try to locate the user's certificate in Active Directory Domain
Services.
/FLUSHCACHE
Clears the calling user's EFS key cache on the specified server.
If servername is not provided, cipher clears the user's key cache
on the local machine.
/REKEY Updates the specified encrypted file(s) to use the configured
EFS current key.
/REMOVEUSER
Removes a user from the specified file(s). CERTHASH must be the
SHA1 hash of the certificate to remove.
请注意,如果您从IRB运行此程序,则每次运行时都需要退出并重新启动IRB。