Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Ruby 中的以下方法可以帮助扫描带有哈希的输入字符串吗?
def foo(bar) (bar.scan(/^#/).empty?) ? "##{bar}" : bar end
您可以通过以下方式实现相同的目标:
if bar.start_with?('#') bar else "##{bar}" end
或使用三元:
bar.start_with? '#' ? bar : "##{bar}"