我有一个带有类变量的模块
module Abc
@@variable = "huhu"
def self.get_variable
@@variable
end
class Hello
def hola
puts Abc.get_variable
end
end
end
a = Abc::Hello.new
a.hola
Run Code Online (Sandbox Code Playgroud)
是否可以不使用方法进入@@variable内部?我的意思是说会很好.只是好奇.Helloget_variableAbc.variable
有没有一种简单的方法将字符串的第一个字母转换为小写?String#capitalize修改整个字符串.当然,我可以删除第一个字母,将其缩写,然后将其添加到开头.但它似乎有点傻,有更简单的方法吗?
注意:我将只处理英文单词.
编辑:str[0] = str[0].downcase在JRuby 1.6中不起作用:(
编辑2:最后我决定这个:
word = "ABC"
first_capital_letter = word.match(/^([A-Z])/).to_s
if(first_capital_letter)
word = word.sub(first_capital_letter, first_capital_letter.downcase)
puts word
end
Run Code Online (Sandbox Code Playgroud)