Ruby 无缩进的多行字符串

Ruby 无缩进的多行字符串,ruby,multilinestring,Ruby,Multilinestring,如何使一个没有前导空格的多行字符串仍然与方法正确对齐?以下是我的一些尝试。正在工作的那个不是很有趣 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

如何使一个没有前导空格的多行字符串仍然与方法正确对齐?以下是我的一些尝试。正在工作的那个不是很有趣

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 properly"+
    "formatted but isn't there a better way?"
  end
end
更新

这里有一个:

code=您可以使用一个HEREDOC——如下所示:

def welcome
  <<-"welcome".strip_heredoc
    "
    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. :(
    "
  welcome
end

我不确定我是否理解这个问题。这个(纯Ruby)解决方案能满足您的需求吗

arr = <<-BITTER_END.split("\n")
  Hello
         Testing, testing.
       I assume the least-indented line is to be left-adjusted and the same amount of leading whitespace is to be removed from all other lines.
BITTER_END
undent = arr.map { |line| line[/^\s*/].size }.min
str = arr.map { |s| s[undent..-1] }.join("\n")

puts str
Hello
     Testing, testing.
   I assume the least-indented line is to be left-adjusted and the same amount of leading whitespace is to be removed from all other lines.
arr=在中,Avdi Grimm描述了一种从多行字符串中去除前导空格的技术:

def unindent(s)
  s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, '')
end
它的行为与此问题的其他现有解决方案兼容,例如在ActiveSupport/Rails或单机版中

您可以将此方法与ruby(以及许多其他语言)中的特殊语法结合使用,以编写多行字符串

module Something
  def unindent(s)
    s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, '')
  end

  def welcome
    unindent(<<-TEXT)
      Hello

      This is an example. This multiline string works
        - even with deeper nestings...
      All is OK here :)
    TEXT
  end
end
modulesomething
def unindent(s)
s、 gsub(/^{s.scan(/^[\t]+(?=\s)/).min}/,“”)
结束
欢迎光临


unindent(从Ruby 2.3.0开始,有一个内置的方法:[
我想你不能。这可能会有帮助。它是否“有趣”重要吗?是的!我认为样式指南中的解决方案是好的。虽然Active Record实现了
strip\u herdoc
,这减轻了痛苦,但它并不能缓解向块中添加新内容的长期问题。使用
可以很容易地看到边距的位置。当multiple people在一个项目上工作。不过,我会将模式稍微更改为
/^\s*\\\\/
,以允许它工作,即使块未从左边缘缩进。看起来像是关于引导空格圈的问题!抱歉,我遗漏了一个重要部分。答案已更新。直到
strip\u herdoc
\_(ツ)_/“@m8ss-
strip\u herdoc
是Rail的一部分,因此它很容易添加到任何Ruby脚本中。核心扩展非常有用,应该是任何“普通”脚本的一部分。”Rubyist工具箱。核心扩展的想法是,如果使用
require'active\u support/core\u ext'
,则不必拉入整个库。相反,使用gem的显式路径,只选择您想要的添加项:
require'active\u support/core\u ext/string/strip'
。这是在每个AS方法的文档。如果有前导空格数量不同的行,则会中断。通常应保留此项,以仅删除所有行共有的部分空格。谢谢,@Holger。正如我所怀疑的,我误解了这个问题。我编辑了我的答案。虽然我可以轻松地使用
active_support/core_ext
,我将使用这种方法。我喜欢它的干燥和直截了当,我可以少依赖一个。Avdi就是那个人。谢谢@Holger提出这个问题。@m8ss既然你提到干燥,我只想指出它实际上会更干燥(而且更干净一点)将该方法添加到
字符串
类中。这样,您就可以在任何地方使用它,而不需要一个可能与您要在中使用
unindent
方法的模块无关的模块。添加的额外功能:与
unindent
一样,它不是一个添加的依赖项-只是您自己添加到
St中的方法铃声
。太棒了。谢谢你的帮助@jeffdill2。我一定会继续使用它。我感谢你自愿抽出时间来帮助程序员同事!tabs不会放弃吗?tab是ASCII 9,而空格是32,所以
[“”,“\t”].min\t=“\t”
。(在
“\t”
之后应该有10个空格,但会缩小。)好吧,如果你在你的公共前缀中混合了制表符和空格,这样你就有了带有不同空格前缀的行,那么所有的赌注都是无效的,你应该得到你应得的混乱。所以不要使用它,坚持接受的ruby标准,即2个空格缩进。在任何情况下:最好强调结尾的
EOS
不需要也可以放在行的开头,这样周围的缩进就可以很好地保留。太棒了!非常感谢!ruby 3的语法更干净、更优雅?
def unindent(s)
  s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, '')
end
module Something
  def unindent(s)
    s.gsub(/^#{s.scan(/^[ \t]+(?=\S)/).min}/, '')
  end

  def welcome
    unindent(<<-TEXT)
      Hello

      This is an example. This multiline string works
        - even with deeper nestings...
      All is OK here :)
    TEXT
  end
end
indented = 
<<-EOS
  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. :(
EOS

unindented =
<<~EOS
  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. :(
EOS

puts indented #=>

  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. :(

puts unindented #=>

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. :(