programing

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

itmemos 2023. 6. 16. 21:27
반응형

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

문자열 배열에 대한 문자 그대로의 표현이 좋습니다.

%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/

Ruby의 공식 문서에는 다음과 같이 나와 있습니다.

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

반응형