Using %I and %I Symbol Array Literal

Using %i and %I symbol array literal

I'm having trouble finding what the %i does in relation to a symbol array.

It is an array literal for an array of symbols. It does the same thing in relation to symbol arrays as ' does to strings.

What does %i or %I do in Ruby?

%i[ ] # Non-interpolated Array of symbols, separated by whitespace
%I[ ] # Interpolated Array of symbols, separated by whitespace

The second link from my search results http://ruby.zigzo.com/2014/08/21/rubys-notation/

Examples in IRB:

%i[ test ]
# => [:test]
str = "other"
%I[ test_#{str} ]
# => [:test_other]

Is there a literal notation for an array of symbols?

Yes! This is possible now in Ruby 2.0.0. One way to write it is:

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

You can also use other delimiters, so you could also write %i(foo bar) or %i!foo bar! for example.

This feature was originally announced here:

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

It is mentioned in the official documentation of Ruby here:

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

What does %w(array) mean?

%w(foo bar) is a shortcut for ["foo", "bar"]. Meaning it's a notation to write an array of strings separated by spaces instead of commas and without quotes around them. You can find a list of ways of writing literals in zenspider's quickref.

Create array of symbols

The original answer was written back in September '11, but, starting from Ruby 2.0, there is a shorter way to create an array of symbols! This literal:

%i[address city state postal country]

will do exactly what you want.

Sublime Text 3 Doesn't Recognize %i in ruby

The %i[foo bar] # [:foo :bar] literal notation for a symbol array was only implemented in Ruby 2.0, while the Ruby language definition that ships with ST3 is mostly focused on 1.9 and earlier. I searched around a bit, but unfortunately I couldn't find any .tmLanguage files that are updated for 2.0, let alone include this literal notation, so I can't point you to a ready-made solution. But, I do have a few suggestions.

First, head over to the unofficial Sublime Text Issues tracker and post a bug report. We're not sure how much attention is paid to this list by the developer, but it at least broadens the issue's visibility and may prompt someone to post a fix. You can also reply to this thread on the Sublime Text forum and perhaps reference your issue.

The second option, if you have good regex-fu, is to hack the Ruby.tmLanguage file and add support yourself. I was going to post directions on how to do it, but then I tried it myself and it seemed to work, so feel free to use my work:

  1. Go to Preferences -> Browse Packages to open up the Packages folder in your system's file explorer.
  2. Create a folder called Ruby2.
  3. Copy the contents of this gist into a new file, and save it in your Ruby2 directory as Ruby2.tmLanguage.
  4. Restart Sublime, switch to your problematic code, and select View -> Syntax -> Ruby2. Both lines should now be highlighted the same way. Here's a before and after screenshot using the Neon Color Scheme:

Ruby literal before and after

I hope this helps. I'm not a Rubyist, so if I made any blatant errors please let me know.

From my (brief) research there definitely seems to be a need for an updated version of Ruby.tmLanguage for all the new features in 2.0, so hopefully any issues you post will prompt someone to start/publish a project. I've already done something similar for Python, but my Ruby skillz just aren't there for this project :)

Good luck!

Ruby arrays: %w vs %W

%w quotes like single quotes '' (no variable interpolation, fewer escape sequences), while %W quotes like double quotes "".

irb(main):001:0> foo="hello"
=> "hello"
irb(main):002:0> %W(foo bar baz #{foo})
=> ["foo", "bar", "baz", "hello"]
irb(main):003:0> %w(foo bar baz #{foo})
=> ["foo", "bar", "baz", "\#{foo}"]

Ruby: how to initialize an array across several lines

You will want to put the comma, after the item like so

myarray = [
"string 1",
"string 2",
"string 3"
]

Also, if you might be thinking of putting the comma before the item, for say easy commenting or something like that while your working out your code. You can leave a hanging comma in there with no real adverse side effects.

myarray_comma_ended = [
"test",
"test1",
"test2", # other langs you might have to comment out this comma as well
#"comment this one"
]

myarray_no_comma_end = [
"test",
"test1",
"test2"
]


Related Topics



Leave a reply



Submit