Tag:

Ruby MetaProgramming中令人困惑的案例

我是Ruby MetaProgramming的初学者。 在irb中练习我的代码时,我遇到了这个问题。 class A; end a = A.new b = class < works for b.foo b.instance_eval { define_method :bar do; puts ‘bar’; end } # => WHY this one works for a.bar rather than b.bar 最后一段代码片段让我困惑。 感谢您的具体答案,但也许我没有清楚地解释我的困惑。 我真正想要理解的是为什么define_method在这些情况下表现得如此不同,这里: class A def foo1 p ‘foo1 from A’ end define_method :bar1 do p ‘bar1 from A’ […]

如何将类包含在类中

如何获取类中包含的模块数组,不包括那些通过inheritance传入的模块? 请注意, ancestors , included_modules将不起作用,因为它们的结果不区分包含在超类中预先添加的模块的模块。 换句话说,他们无法区分以下两种情况: M置于B的超类 class A; end class B [B, M, A, Object, Kernel, BasicObject] B M # => -1 B.included_modules # => [M, Kernel] M包含在B class A; end class B [B, M, A, Object, Kernel, BasicObject] B M # => -1 B.included_modules # => [M, Kernel]

如何在ruby中清除rspec测试之间的类变量

我有以下类:我想确保类url仅为所有实例设置一次。 class DataFactory @@url = nil def initialize() begin if @@url.nil? Rails.logger.debug “Setting url” @@url = MY_CONFIG[“my value”] end rescue Exception raise DataFactoryError, “Error!” end end end 我有两个测试: it “should log a message” do APP_CONFIG = {“my value” => “test”} Rails.stub(:logger).and_return(logger_mock) logger_mock.should_receive(:debug).with “Setting url” t = DataFactory.new t = nil end it “should throw an […]

如何获取其值等于ruby中给定参数的哈希的键?

所以我试图从rubeque http://www.rubeque.com/problems/related-keys-of-hash/解决这个问题。 Basicaly我只需要得到哈希的键,其值等于给定的参数。我想知道你们是否可以给我一些提示? 解决这个问题,非常感谢你 这就是我到目前为止所拥有的 class Hash def keys_of(*args) key = Array.new args.each { |x| key << x} key.each { |x,y| x if y == key} end end assert_equal [:a], {a: 1, b: 2, c: 3}.keys_of(1) assert_equal [:a, :d], {a: 1, b: 2, c: 3, d: 1}.keys_of(1) assert_equal [:a, :b, :d], {a: 1, b: 2, […]

如何检索class级名称?

我正在使用Ruby on Rails 3.0.7,我想检索类名,如果它是命名空间的话。 例如,如果我有一个名为User::Profile::Manager我将使用一些未知的Ruby或Ruby on Rails方法并以安全的方式从中检索Manager字符串。 顺便说一句 :我可以获得哪些“常用”的“有用”信息?

Sinatra请求对象

我可能在这里遗漏了一些非常明显的东西,但我似乎无法找到答案,或者自己解决这个问题。 在Sinatra中,他们有一个self.get方法,它捕获块,当一个块被调用时,你可以在里面使用request变量,这怎么可能? 西纳特拉 module Sinatra class Base class Request < Rack::Request end attr_accessor :request def call!(env) @request = Request.new(env) end class << self def get(path, opts = {}, &block) … end end end end 应用 class App < Sinatra::Base get '/' do puts request end end

使用独立代码扩展Ruby类

我有一个Rails应用程序,其中包含几个具有相同结构的模型: class Item1 true end class Item2 true end 实际代码更复杂,但这足以简化。 我想我可以将代码的公共部分放在一个地方,然后在所有模型中使用它。 以下是我的想法: class Item1 true end 显然它不起作用有两个原因: CommonItem不知道我调用的类方法。 在CommonItem而不是Item1中查找WIDTH和HEIGHT常量。 我尝试使用include而不是extend , class_eval和类inheritance的某些方法,但没有一个工作。 我似乎错过了一些明显的东西。 请告诉我什么。

如何在初始化父类时返回子类的新实例?

给定一个类层次结构如下: class A def initialize(param) if param == 1 then #initialize and return instance of B else #initialize and return instance of C end end end class B < A end class C < A end 初始化A时是否可以实际初始化并返回B或C的实例? 即my_obj = A.new(param)将导致my_obj成为B类或C类的实例,具体取决于param的值,该值在A.initialize(param)进行检查。 在我的用例中,它在运行时只知道使用哪个子类( B或C )和父类( A )基本上从未真正使用过。 我认为将B或C决定是否为共同祖先的逻辑可能是个好主意。 如果这是不可能的(或不好的风格),我应该在哪里检查param和决定初始化哪个类?

在类ruby之间共享实例变量

我知道这个问题之前已经得到了回答,但我似乎无法使任何解决方案都有效 我正在为我需要调用的API创建一个ruby包装器。 Interface类完成了api的所有会话处理和实际调用,但是我想为我将最常执行的函数构建帮助器类。 我遇到的问题是我需要一种方法来跨多个辅助类维护一个Interface类的实例。 这是我到目前为止的代码 require_relative ‘interface’ module Api_Helper attr_accessor :xmlmc #get a new instance of the interface, This should be the only instance used. #I don’t know if this needs to go in its own module def initialize server, port = ‘5015’ @xmlmc = Xmlmc::Interface.new server, port end end module Xmlmc class API include […]

Rails – 如何从lib目录中调用方法?

我在lib目录中有这个方法(文件my_class_name.rb ): class MyClassName def doSomething … end … end 在控制器中: class UsersController < ApplicationController require 'my_class_name' def show_stats ::MyClassName.doSomething() end end 回报 MyClassName的未定义方法`doSomething’:Class 如何正确调用此方法?