English 中文(简体)
我想要在这里使用什么比拉变量?
原标题:What kind of Ruby variable do I want to use here?

I’m still learning Ruby, and I’m curious about whether it is appropriate to use a class variable, constant, or local variable in this scenario.

In my below code example (that generates random usernames out of a fixed character set), assigning @username as an instance variable is fairly obvious. But I’m curious whether I should assign the charset as a constant or maybe a class variable. What would be the advantages of using another variable type in this scenario?

In the current example, the _charset is computed in every instance. (Do correct me if my assumption is wrong.) I also assume the computation would be shared between instances (as opposed to recomputed) as both a class variable and as a constant?

class NewAccount

  def initialize
    @username = self.generate_username
  end

  def generate_username
    _charset = ( a .. z ).to_a + ( 0 .. 9 ).to_a
    _newusername = Array.new(20) { _charset[rand(_charset.length)] }.join
    return _newusername
  end

end
最佳回答

由于_charset = (a . z ).to_a + ( 0 . 9 ).to_a 从定义的角度看,I d 将其作为一个类别不变:

class NewAccount

  CHARSET = ( a .. z ).to_a + ( 0 .. 9 ).to_a

  def initialize
    @username = self.generate_username
  end

  def generate_username
    _newusername = Array.new(20) { CHARSET[rand(CHARSET.length)] }.join
    return _newusername
  end

end
问题回答

你们可以使其成为一种类别变数或不变的,实际上,情况相同:只有一种情况存在。 采购处的常数不是实际不变的,因此,你可以改变常数,这样实际上就是一个变数,因为如果你试图改变它的话,你就认识到一种不变的、发出警告。

但是,通过宣布它为不变的,而不是类别变数,你正在记录你的目标:具有固定价值,包括一套特性,但并非要改变。 阅读该守则的人都清楚这一点。 这就是你们想要的特性,也是这样。

如果你使这个类别变异,那将是一个变数:如果有人试图改变,那么就不会有问题。 当然,如果你计划以任何理由改变其价值,那就是一个类别变量:你将再次记录你的设计。

我认为,由于“果园”将只用于“新债务”类别,而其价值不会因“新债务”的情况而改变,你可以使其类别变异。

举例来说,“用户名称”每例都会计算一次,而“果园”只计算一次,而“果园”只是一个局部变量,因此,如果你掌握了两种方法的话,就会重新计算。

你们想要的是Tin Man建议,把它固定下来,一劳永逸。 采用一种可变的等级(@charset)可能会产生误导,因为果园无意在任何时候改变。





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签