What is the %w “thing” in ruby?

前端 未结 4 1428
闹比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 16:52

    There are various forms of literal syntax you can use, with pretty much any paired delimiter, see http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html:

    %w[one two three] # == ["one", "two", "three"]
    
    %[one two three] # == "one two three"
    %[one "two" three] # == "one \"two\" three"
    
    %r[one two three] # == /one two three/
    

提交回复
热议问题