What is the %w “thing” in ruby?

前端 未结 4 1401
闹比i
闹比i 2020-12-28 16:41

I\'m referring to the %w operator/constructor/whatever you may call it, used like this:

%w{ foo bar baz }
=> [\"foo\", \"bar\", \"baz\"]

4条回答
  •  时光说笑
    2020-12-28 17:07

    Unsure about the "official" documentation but this is pretty good : http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_.25_Notation

    Whether you use {} [] () or <> does not matter except if your string contains this character e.g.:

    %q{a closing parenthesis: ")"}
    

    The syntax is pretty complex so remembering every variant is not very useful, but it can come in handy when you are hacking quickly and want to avoid taking care of escape characters manually.

提交回复
热议问题