草庐IT

通用 Perl 模块的 Python 等价物?

全部标签

ruby-on-rails - 不理解类、模块和类 << self 方法

我有以下代码:classMyClassmoduleMyModuleclass当我调用方法时myfunction像这样,它工作正常:>me=MyClass::MyModule.myfunction=>"Nathan">me=>"Nathan"但是如果我删除了class并添加self.myfunction的前缀,它不起作用。例如:classMyClassmoduleMyModuleattr_accessor:first_namedefself.myfunctionMyModule.first_name="Nathan"endendend>me=MyClass::MyModule.myfun

ruby - 在类中包含模块并执行代码

这是我以前上过的课classSomething#Definesthevalidatesclassmethods,whichiscalleduponinstantiationincludeModulevalidates:namevalidates:dateend我现在有几个使用相同功能的对象,更糟糕的是,有几个定义相似事物的对象,如下所示:classAnotherthing#Definesthevalidatesclassmethods,whichiscalleduponinstantiationincludeModulevalidates:ageend我想“重用”这些类的内容,所以我把

ruby - 引用与类同名的模块中的方法

我有一个同名的类和一个模块:modulePushoverdefconfigure..endendmoduleMyModuleclassPushoverdefblahPushover.configureendendend这不起作用,因为Pushover.configure调用指向包含类。现在,一个明显的解决方法是重命名该类。但是,模块来自gem,并且类符合DSL中要求的命名约定。所以理想情况下,它们应该保持不变。我还可以创建第二个帮助程序类并通过它进行调用,但这一切看起来有点老套。我的首选解决方案是直接引用模块方法。围绕这个领域的所有现有问题似乎都在相反的方向上消除歧义——即他们想要获得

Ruby 相当于 Python setattr()

好吧,将我添加到爱上Ruby但对PyAddiction挥之不去的Python程序员的列表中。喜欢关于Python'sgetattr的帖子,我正在寻找与此等效的Ruby:setattr(obj,'attribute',value)其中obj是一个对象实例,attribute是对象属性之一的字符串名称,value是该对象的值。等效代码为:obj.attribute=value我假设这是可能的(因为现在在Python中的任何可能在Ruby中似乎更容易),但找不到它的文档。 最佳答案 obj.instance_variable_set("@

ruby-on-rails - 模块的 Rails 未定义方法

在Rails中,如何使用模块中的特定方法。例如,#./app/controllers/my_controller.rbclassMyControllerMyController包含MyModule。在action中,我想使用MyModule.a_method(请注意我在MyController中也有一个私有(private)的a_method并且我不我想用这个。)我尝试过的事情:1)将模块中的方法定义为self.defself.a_methodend2)在Controller中使用::表示法(MyModule::a_method)我不断收到的错误是MyModule:module的未定义

ruby - 如何改进 Ruby 中的模块方法?

您可以使用优化您的类(class)moduleRefinedStringrefineStringdodefto_boolean(text)!!(text=~/^(true|t|yes|y|1)$/i)endendend但是如何细化模块方法呢?这:moduleRefinedMathrefineMathdodefPI22/7endendend引发:TypeError:错误的参数类型模块(预期类) 最佳答案 这段代码可以工作:moduleMathdefself.piputs'originalmethod'endendmoduleRefin

ruby - 在 Ruby 中编写 C 接口(interface)比在 Perl 中更容易吗?

根据officialrubyAboutpage用C扩展Ruby比用Perl更容易。我不是(perl)XS专家,但我发现使用Inline::C快速简单地编写一些东西非常简单,那么为什么在Ruby中更容易呢?WritingCextensionsinRubyiseasierthaninPerlorPython,withaveryelegantAPIforcallingRubyfromC.ThisincludescallsforembeddingRubyinsoftware,foruseasascriptinglanguage.ASWIGinterfaceisalsoavailable.那些做

ruby-on-rails - Ruby on Rails 4 查找等价的订单和限制

我需要非常快地将我的应用程序Controller更新到Rails4,但我不知道如何以新的Rails4样式编写所有这些行:使用带有条件和限制的order_by,在网络中我搜索快速,并且所有部分都在一起-没找到。我如何将其从Rails3转换为Rails4?在我的Controller中@news=Article.find(:all,conditions:{text:"example"},order_by:"created_at",limit:4)我不必在4中使用条件,只在where中使用,但是如何附加:all、order_by和limit呢?什么是正确的语法? 最

ruby - 在 Ruby 的类中访问模块的类变量

我有一个包含类变量的模块moduleAbc@@variable="huhu"defself.get_variable@@variableendclassHellodefholaputsAbc.get_variableendendenda=Abc::Hello.newa.hola是否可以在Hello中获取@@variable而无需使用get_variable方法?我的意思是像Abc.variable这样的东西会很好。只是好奇。 最佳答案 您不能在模块的Hello类范围内直接访问@@variable(即Abc.variable)>Abc

ruby - 如何在 ruby​​ 中组合包含 method_missing 的模块

我有几个模块缺少扩展方法:moduleSaysHellodefrespond_to?(method)super.respond_to?(method)||!!(method.to_s=~/^hello/)enddefmethod_missing(method,*args,&block)if(method.to_s=~/^hello/)puts"Hello,#{method}"elsesuper.method_missing(method,*args,&block)endendendmoduleSaysGoodbyedefrespond_to?(method)super.respond_