반응형
기호 배열에 문자 표기법이 있습니까?
문자열 배열에 대한 문자 그대로의 표현이 좋습니다.
%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
반응형
'programing' 카테고리의 다른 글
| mariadb 서버에서 어레이 래퍼를 제거하는 방법 (0) | 2023.06.16 |
|---|---|
| R에서 가장 독특한 색상을 생성하는 방법은 무엇입니까? (0) | 2023.06.16 |
| 전월 Oracle 날짜 함수 (0) | 2023.06.16 |
| iPhone의 세로에 있는 UISplitViewController에 마스터 대신 세부 VC가 표시됨 (0) | 2023.06.16 |
| iPhone Simulator - 느린 연결을 시뮬레이션하시겠습니까? (0) | 2023.06.16 |