Programming

기호 배열에 대한 문자 표기법이 있습니까?

procodes 2020. 5. 29. 23:12
반응형

기호 배열에 대한 문자 표기법이 있습니까?


나는 문자열 배열에 대한이 리터럴 표현식을 좋아합니다.

%w( i can easily create arrays of words )

기호 배열을 얻는 리터럴이 있는지 궁금합니다. 내가 할 수 있다는 걸 알아

%w( it is less elegant to create arrays of symbols ).map( &:to_sym )

그러나 리터럴을 사용하는 것만으로도 정말 훌륭합니다.


예! 이것은 이제 Ruby 2.0.0에서 가능합니다. 작성하는 한 가지 방법은 다음과 같습니다.

%i{foo bar}  # => [:foo, :bar]

당신은 또한 쓸 수 있도록 당신은 또한, 다른 구분 기호를 사용할 수 있습니다 %i(foo bar)또는 %i!foo bar!예를 들어.

이 기능은 원래 여기에 발표되었습니다.

http://www.ruby-lang.org/zh_TW/news/2012/11/02/ruby-2-0-0-preview1-released/

루비 공식 문서에 나와 있습니다.

http://ruby-doc.org/core/doc/syntax/literals_rdoc.html#label-Percent+Strings


불행히도 Ruby 1.x에서는 사용 가능한 % 구분 기호 목록 이 제한되어 있습니다.

Modifier    Meaning
%q[ ]       Non-interpolated String (except for \\ \[ and \])
%Q[ ]       Interpolated String (default)
%r[ ]       Interpolated Regexp (flags can appear after the closing delimiter)
%s[ ]       Non-interpolated Symbol
%w[ ]       Non-interpolated Array of words, separated by whitespace
%W[ ]       Interpolated Array of words, separated by whitespace
%x[ ]       Interpolated shell command

참고 URL : https://stackoverflow.com/questions/8816877/is-there-a-literal-notation-for-an-array-of-symbols

반응형