这是一本书的例子:
class TextCompressor
attr_reader :unique, :index
def initialize( text )
@unique = []
@index = []
add_text( text )
end
def add_text( text )
words = text.split
words.each { |word| add_word( word ) }
end
def add_word( word )
i = unique_index_of( word ) || add_unique_word( word )
@index << i
end
def unique_index_of( word )
@unique.index(word)
end
def add_unique_word( word )
@unique << word
unique.size - 1
end
end
Run Code Online (Sandbox Code Playgroud)
在该方法中add_unique_word,作者unique不使用@符号(unique.size - 1)访问变量.怎么可能,为什么会这样呢?
有一个很好的方法,parameterize但它使字符串小写.那么有没有一种方法来参数化字符串而不使其小写?