你如何拥有一个没有前导空格的多行字符串,并且仍然与方法正确对齐?以下是我的一些尝试.工作的那个不是很有趣......
module Something
def welcome
"
Hello
This is an example. I have to write this multiline string outside the welcome method
indentation in order for it to be properly formatted on screen. :(
"
end
end
module Something
def welcome
"
Hello
This is an example. I am inside welcome method indentation but for some reason
I am not working...
".ljust(12)
end
end
module Something
def welcome
"Hello\n\n"+
"This is an example. I am inside welcome method indentation and …Run Code Online (Sandbox Code Playgroud) Rubocop确认了《 Ruby样式指南》。它不鼓励使用实例变量之外的任何东西。我发现不至少使用类变量令人困惑。样式指南中的此代码片段不赞成使用全局变量,而是建议使用模块实例变量:
# bad
$foo_bar = 1
# good
module Foo
class << self
attr_accessor :bar
end
end
Foo.bar = 1
Run Code Online (Sandbox Code Playgroud)
谨慎使用全局变量是有道理的,但是既不使用全局变量也不使用类变量会让我大吃一惊。
在模块实例变量和类实例变量中,哪个更有效地使用内存?
例如:
选项A(类实例变量):
# things that exist only with life
module Life
# an instance of life with unique actions/attributes
class Person
attr_accessor :memories
def initialize
@memories = []
end
def memorize(something)
@memories << something
end
end
end
bob = Life::Person.new
bob.memorize 'birthday'
bob.memorize 'wedding'
bob.memorize 'anniversary'
bob.memories
# …Run Code Online (Sandbox Code Playgroud) 这有效(带有斜杠):
config.autoload_paths += Dir[Rails.root.join("lib", "**/")]
Run Code Online (Sandbox Code Playgroud)
这不起作用(不带斜线):
config.autoload_paths += Dir[Rails.root.join("lib", "**")]
Run Code Online (Sandbox Code Playgroud)
为什么?