Tag: 实例变量

声明实例变量迭代哈希!

我想做以下事情: 我想声明迭代字典的类的实例变量。 我们假设我有这个哈希值 hash = {“key1” => “value1″,”key2” => “value2″,”key3” => “value3”} 我希望将每个键作为类的实例变量。 我想知道我是否可以声明迭代遍历该哈希的变量。 像这样的东西: class MyClass def initialize() hash = {“key1” => “value1″,”key2” => “value2″,”key3” => “value3”} hash.each do |k,v| @k = v end end end 我知道这不起作用! 我只把这段代码放在一边看看你是否能理解我想要的更清楚。 谢谢!

迭代并设置Ruby对象实例变量

我正在寻找一种方法来迭代Ruby对象的实例变量,并使用通用setter单独设置它们(从提供的哈希)。 我假设我可以直接在方法中迭代它们并简单地单独设置它们。 这是我的对象实例,你: u = # 我想用给定的哈希填充它,h: h = { “id”=>”141”, “alpha”=>”Muccahiya”” “bravo”=>”$2a$10$xR2g”, “charlie”=>”2018-02-21 10:41:56-05”, “delta”=>”2018-02-05 18:17:16.752606-05”, “echo”=>”wobbly”, “status”=>”active” } 这是我的方法,它抛出这个错误: def to_obj(h) self.instance_variables.each do |i| self.i = h[i.sub(‘@’,”)] end end 错误: Traceback (most recent call last): 3: from /users/rich/app.rb:31:in `’ 2: from /Library/WebServer/Documents/dingbat/models/waffle.rb:240:in `to_obj’ 1: from /Library/WebServer/Documents/dingbat/models/waffle.rb:240:in `each’ /Library/WebServer/Documents/dingbat/models/waffle.rb:241:in `block in to_obj’: no implicit […]

Rails – 自我与@

我正在关注Michael Hartl的RoR教程,它涵盖了密码加密的基础知识。 这是目前的用户模型: class User true, :length => {:maximum => 50} validates :email, :presence => true, :format => {:with => email_regex}, :uniqueness => {:case_sensitive => false} validates :password, :presence => true, :length => {:maximum => 20, :minimum => 6}, :confirmation => true before_save :encrypt_password private def encrypt_password self.encrypted_password = encrypt(password) end def encrypt(string) string end […]

实例变量,类变量以及它们之间的区别在ruby中

我很难理解实例变量,类变量以及它们之间在ruby中的区别……有人可以向我解释它们吗? 我已经完成了大量的谷歌搜索,完全无法理解它们。 谢谢!

是否有可能比较Ruby中的私有属性?

我在考虑: class X def new() @a = 1 end def m( other ) @a == other.@a end end x = X.new() y = X.new() xm( y ) 但它不起作用。 错误消息是: syntax error, unexpected tIVAR 我如何比较来自同一类的两个私有属性呢?

在Ruby中inheritance类级实例变量?

我希望子类从其父级inheritance类级实例变量,但我似乎无法弄明白。 基本上我正在寻找这样的function: class Alpha class_instance_inheritable_accessor :foo # @foo = [1, 2, 3] end class Beta < Alpha @foo << 4 def self.bar @foo end end class Delta < Alpha @foo << 5 def self.bar @foo end end class Gamma < Beta @foo << 'a' def self.bar @foo end end 然后我想要这样输出: > Alpha.bar # [1, 2, […]

Ruby:自动将实例变量设置为方法参数?

是否有任何计划实现类似于在方法参数列表中指定实例变量名称的CoffeeScriptfunction的ruby行为? 喜欢 class User def initialize(@name, age) # @name is set implicitly, but @age isn’t. # the local variable “age” will be set, just like it currently works. end end 我知道这个问题: 在Ruby中,我可以在initialize方法中以某种方式自动填充实例变量吗? ,但所有的解决方案(包括我自己的)似乎都不符合ruby简单哲学。 并且,这种行为会有任何缺点吗? UPDATE 其中一个原因是ruby社区的DRY(不要重复自己)哲学。 我经常发现自己需要重复参数变量的名称,因为我希望将它分配给同名的实例变量。 def initialize(name) # not DRY @name = name end 我能想到的一个缺点是,如果一个方法没有正文,它看起来好像什么也没做。 如果您正在快速扫描,这可能看起来像一个无操作。 但我认为,只要有时间,我们就可以适应。 另一个缺点:如果您在正文中设置其他实例变量,并且您尝试通过将所有分配放在开头可读,则可能需要更多的认知“权力”才能看到分配也发生在参数列表中。 但我不认为这比看到一个常量或方法调用并且不得不跳到它的定义更难。 # notice: instance var […]

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’, […]

在Ruby中,如何在哈希而不是数组中获取实例变量?

我有一个Ruby类。 我想从参数中获取一个实例变量到该类中的方法。 我可以将所有实例变量作为数组获取: self.instance_variables 但是,我想获取名为arg的实例变量,具体如下: class MyClass def get_instance_variable(arg) hash_of_instance_variables[arg] end end object.get_instance_variable(‘my_instance_var’) 我如何计算hash_of_instance_variables ?

Ruby中的对象赋值

来自c ++背景我很好奇Ruby中的对象赋值。 应对以下对象分配进行哪些考虑(如果有): class MyClass attr_accessor :a, :b def initialize(a, b) @a = a @b = b end def some_method puts “#{self.a} #{self.b}” end end m = MyClass.new(“first”, “last”) n = MyClass.new(“pizza”, “hello”) q = n q.some_method