gpt4 book ai didi

ruby - 没有缩进的多行字符串

转载 作者:数据小太阳 更新时间:2023-10-29 06:47:45 24 4
gpt4 key购买 nike

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

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

更新

这是一个 method from the ruby style guide :

code = <<-END.gsub(/^\s+\|/, '')
|def test
| some_method
| other_method
|end
END
# => "def test\n some_method\n other_method\nend\n"

最佳答案

从 Ruby 2.3.0 开始,有一个内置的方法:[ <<~ ]

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

Multiline strings in Ruby 2.3 - the squiggly heredoc

关于ruby - 没有缩进的多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33527064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com