Tag:

Rubocop错误’类定义太长ruby’

我得到rubocop错误’类定义太长。 [100分之236]”。 我的课程如下: class SomeClassName include HelperModule attr_accessor :aaa, :bbb, :ccc …. methods ….. end 可能出什么问题? rubocop docs ClassLength说“类的长度超过某个最大值”。 这是什么意思?

Ruby类实例变量和inheritance

我有一个名为LibraryItem的Ruby类。 我想将此类的每个实例与一组属性相关联。 这个数组很长,看起来像 [‘title’, ‘authors’, ‘location’, …] 请注意,这些属性实际上不应该是方法,只是LibraryItem具有的属性列表。 接下来,我想创建一个名为LibraryBook的LibraryItem的子类,它具有一个属性数组,其中包含LibraryItem所有属性,但也包含更多属性。 最后,我将需要LibraryItem几个子类,每个子类都有自己的数组@attributes但每个子类都添加到LibraryItem的@attributes (例如, LibraryBook , LibraryDVD , LibraryMap等)。 所以,这是我的尝试: class LibraryItem < Object class << self; attr_accessor :attributes; end @attributes = ['title', 'authors', 'location',] end class LibraryBook < LibraryItem @attributes.push('ISBN', 'pages') end 这不起作用。 我收到了错误 undefined method `push’ for nil:NilClass 如果要工作,我会想要这样的东西 puts LibraryItem.attributes puts LibraryBook.attributes 输出 [‘title’, ‘authors’, […]

动态创建一个类

我正在尝试创建一个新类,不知道该类的名称,直到它应该创建。 像这样的东西; variable = “ValidClassName” class variable end Test = ValidClassName.new 如果可能的话,我也会欣赏有关如何动态地向这个新类添加属性(和方法)的提示。 我将为课程检索“设置”,它们看起来像这样: title :Person attribute :name, String attribute :age, Fixnum 但是不应该只接受那个显式文件,属性可能在数字结尾类型上有所不同。 最终会生成一个看起来像这样的类: class Person def initialize(name, age) @name_out = name @age_out = age end end 救命?

使用不同的参数和默认值在Ruby中初始化类的最有效方法是什么?

我想有一个类和一些属性,您可以在初始化期间设置或使用其默认值。 class Fruit attr_accessor :color, :type def initialize(color, type) @color=color ||= ‘green’ @type=type ||=’pear’ end end apple=Fruit.new(red, apple)

是否可以为子模块提供与顶级类相同的名称?

背景: ruby认为即使我指定了完整的命名空间,我也引用了顶级常量 如何在ruby中引用子模块的“完整路径”? 这是问题,简化为最小的例子: # bar.rb class Bar end # foo/bar.rb module Foo::Bar end # foo.rb class Foo include Foo::Bar end # runner.rb require ‘bar’ require ‘foo’ ➔rubyrunner.rb ./foo.rb:2:warning:Foo :: Bar引用的toplevel常量Bar ./foo.rb:2:in`include’:错误的参数类型Class(expect Module)(TypeError) 来自./foo.rb:2 来自runner.rb:2:在`require’中 来自runner.rb:2

在String和Classname之间转换

我有一个包含类名的字符串。 例如,它是包含“Article”的字符串。 该字符串来自params []。 我该怎么做才能使用这个字符串,好像它是一个类名? 例如,我想做: Article.all 等等。 任何的想法?

是否有类似于inheritance的类#的钩子只在Ruby类定义之后触发?

在class Foo语句之后#inherited调用#inherited 。 我想要的东西只能在关闭类声明的end语句之后运行。 这里有一些代码来举例说明我的需求: class Class def inherited m puts “In #inherited for #{m}” end end class Foo puts “In Foo” end puts “I really wanted to have #inherited tiggered here.” ### Output: # In #inherited for Foo # In Foo # I really wanted to have #inherited tiggered here. 这样的事情存在吗? 可以创建吗? 我完全没有运气吗?

Ruby模块中的私有类(不是类方法)?

我是Ruby的新手(熟悉Python,C ++和C)。 我需要创建一个只能由模块中的其他类和方法使用的类。 在Python中,我只是将其称为__classname。 我在C ++中使用了一个空的typedef。 我如何在Ruby中执行此操作(或者我是在咆哮错误的树而不是以“Ruby方式”执行此操作?)

Ruby – 动态地向类添加属性(在运行时)

我正在寻找一种方法在运行时向我已经定义的类添加属性,或者更好: class Client attr_accessor :login, :password def initialize args = {} self.login = args[:login] self.password = args[:password] end end 但是,我有这个哈希 {:swift_bic=>”XXXX”, :account_name=>”XXXX”, :id=>”123″, :iban=>”XXXX”} 我希望这个哈希成为我的客户端实例的一部分 client = Client.new :login => ‘user’, :password => ‘xxxxx’ 然后带着神奇的魔力 client @@%$%PLIM!!! {:swift_bic=>”XXXX”, :account_name=>”XXXX”, :id=>”123″, :iban=>”XXXX”} 我可以访问 client.swift_bic => ‘XXXX’ client.account_name => ‘XXXX’ client.id => 123 我还想保持一个适当的对象结构,如: Client.new(:login => ‘user’, […]

如何反转ruby的include函数

我将在代码中解释我正在寻找的内容,因为这可能是最简洁的: module Mixin def method puts “Foo” end end class Whatever include Mixin end w = Whatever.new w.method => “Foo” # some magic here w2 = Whatever.new w.method => NoMethodError 我曾尝试使用remove_const取消定义Mixin模块,但这似乎对Whatever没有任何影响。 我假设#include只是将模块的引用添加到类的方法解析链中 – 但是这种行为与此不一致。 任何人都可以告诉我幕后实际做了什么,以及如何扭转这种情况?