Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何从模块或类继承默认数据_Ruby - Fatal编程技术网

Ruby 如何从模块或类继承默认数据

Ruby 如何从模块或类继承默认数据,ruby,Ruby,我正在开发一个应用程序来自动测试SQL注入漏洞。它目前被命名为Deft,用于一个大学项目 我希望能够从命令行或交互式控制台运行测试。我正在编写几个类的代码。(Deft::Cli,Deft::Console等) 这是我想做的 module Deft module App attr_accessor :origin @origin = { "host" => "localhost", "port" => "80" } end end module Deft

我正在开发一个应用程序来自动测试SQL注入漏洞。它目前被命名为Deft,用于一个大学项目

我希望能够从命令行或交互式控制台运行测试。我正在编写几个类的代码。(
Deft::Cli
Deft::Console
等)

这是我想做的

module Deft
  module App
    attr_accessor :origin
    @origin = { "host" => "localhost", "port" => "80" }
  end
end

module Deft
  class Console
    include App

    def initialize
      puts origin
    end
  end
end
示例已经简化,但关键是默认值(和结构)在
Deft::App
模块中定义


我可以看出的问题是,尽管控制台实例中的
methods.grep(/origin/)
确实给了我
[“origin=”,“origin”]
调用
origin
返回
nil
。而不是我在
Deft::App
中定义的值。它不起作用是有道理的,但我不知道如何使它起作用。

也许我把简化的例子看得太字面了,但是修复它的一种方法是去掉
attr\u accessor
类方法调用,只需将
origin
设置为常量
origin
origin

,也许我把简化的示例也照字面理解了,但是修复它的一种方法是去掉
attr\u accessor
类方法调用,只需将
origin
设置为常量
origin
origin
尝试如下操作:

module Deft
  module App
    attr_accessor :origin
    def init
      @origin = { "host" => "localhost", "port" => "80" }
    end
  end
end

module Deft
  class Console
    include App

    def initialize
      init
      puts origin
    end
  end
end

Deft::Console.new
试着这样做:

module Deft
  module App
    attr_accessor :origin
    def init
      @origin = { "host" => "localhost", "port" => "80" }
    end
  end
end

module Deft
  class Console
    include App

    def initialize
      init
      puts origin
    end
  end
end

Deft::Console.new

感谢每一位加入他们意见的人。我将继续回答我自己的问题,正如我所希望的那样

module Deft
  module App
    @@origin = { "host" => "localhost", "port" => "80" }

    def origin        ; @@origin        ; end
    def origin=(args) ; @@origin=(args) ; end
  end
end

如果有人想使用attr_accesor复制并清理此内容,我很乐意将其编辑回我的问题并接受他们的回答。

感谢所有添加他们输入的人。我将继续回答我自己的问题,正如我所希望的那样

module Deft
  module App
    @@origin = { "host" => "localhost", "port" => "80" }

    def origin        ; @@origin        ; end
    def origin=(args) ; @@origin=(args) ; end
  end
end
如果有人想用attr_accesor复制并清理此内容,我很乐意将其编辑回我的问题并接受他们的回答。

这是怎么回事

module Deft
  class DefaultConsole
    attr_accessor :origin
    def initialize
      @origin = {'host' => 'localhost', 'port' => 80}
    end
  end

  class Console < DefaultConsole
    def initialize
      super
      puts origin
    end
  end
end

Deft::Console.new
# => {'host' => 'localhost', 'port' => 80}
模块灵活
类DefaultConsole
属性存取器:源
def初始化
@原点={'host'=>'localhost','port'=>80}
终止
终止
类控制台<默认控制台
def初始化
超级的
起源
终止
终止
终止
Deft::Console.new
#=>{'host'=>'localhost','port'=>80}
这个怎么样

module Deft
  class DefaultConsole
    attr_accessor :origin
    def initialize
      @origin = {'host' => 'localhost', 'port' => 80}
    end
  end

  class Console < DefaultConsole
    def initialize
      super
      puts origin
    end
  end
end

Deft::Console.new
# => {'host' => 'localhost', 'port' => 80}
模块灵活
类DefaultConsole
属性存取器:源
def初始化
@原点={'host'=>'localhost','port'=>80}
终止
终止
类控制台<默认控制台
def初始化
超级的
起源
终止
终止
终止
Deft::Console.new
#=>{'host'=>'localhost','port'=>80}

您希望代码能够分配给
Deft::App.origin
,还是保持不变?我需要能够分配它。您希望代码能够分配给
Deft::App.origin
,或者它将保持不变?我需要能够分配它。我需要能够从cli和控制台设置origin中的值。如果它将是一个
散列
,您可以通过
替换
清除
合并
,等方法设置值,即使它被定义为常量。因此,如果这是您唯一关心的问题,您不需要避免此选项。我需要能够从cli和控制台中设置origin中的值,因为它将是一个
散列
,您可以通过
替换
清除
合并
,等方法设置值,即使它被定义为常量。因此,如果这是您唯一关心的问题,您无需避免此选项。谢谢。我遇到了这个问题,但不喜欢
init
调用的想法。谢谢。我遇到了这个问题,但不喜欢
init
调用的想法。在Rails中,可以使用
mattr\u访问器:origin
。但我认为Ruby中没有其他方法。在Rails中,可以使用
mattr\u访问器:origin
。但是我不认为Ruby还有其他的方式。这让我问了一个问题,“为什么我一开始就不这样做?!”这让我问了一个问题,“为什么我一开始就不这样做?!”