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 代码:
words = params[:words].gsub("\n","").gsub("\s","")
有没有更好的方法来做到这一点,因为代码会占用所有空格和换行符并去掉它们?只是好奇是否有更好或更短的方法,以防我在代码中过于重复。
上面的代码确实对我有用,但我是编程新手,如果可能的话,我想以更好/更美观的方式做事。
实际上,仅\s用于匹配任何空白字符应该有效:
\s
"some\n simple demo \nstring \n".gsub(/\s/, "") # => "somesimpledemostring"
words = params[:words].delete("\s\n")