%q-This is a string- %q|this is a string| %q{def fred(a) a.each { |i| puts i } end}
Basically anything following the %q becomes the delimiter for the string. Also you can use % for strings in specific without needing hte q. It is the default delimiter option.
%-example- %(example) %[example]
There is another interesting delimiter: %w
["three", "word", "array"].each do |s| puts s end
### can also be written %w[three word array].each do |s| puts s end ### output: # three # word # array
%s can be used to make a symbol.
%s(meh)
creates :meh as a symbol.
Usefully for regular expression patterns you can do %r
exp = Regexp.new("[abcdefg]") exp = %r([abcdefg]) ### any regular expression
And finally %e can be used to execute a shell command.
%e(echo "hello world")
Also, there's probably more I don't know about. :P
17 Jul, 2009, Chris Bailey wrote in the 2nd comment:
Votes: 0
Another interest thing to note is that strings contained inside " " are slower (not by much) than strings inside ' ', because using double quotes will check to see if it needs to expand variables for interpolation etc..
You may wish to look up the perl man pages, as ruby borrowed quite a bit of this from perl. I'm curious, does it have the "qw" quoting system as well?
@foo = qw(fred bob "joe bob" bo);
In perl, that's a 4 element array… qw( uses whitespace as a delimiter and parens as containment, quite handy actually.
17 Jul, 2009, David Haley wrote in the 6th comment:
Votes: 0
I think that actually qw uses whatever symbol (or maybe some subset of symbols) comes next as a delimiter, so you can write things like qw/aaaaa/, qw:aaa:, etc.
17 Jul, 2009, Chris Bailey wrote in the 7th comment:
In perl, q is quote literal which works like single-quotes, so q:a b: would be the same as 'a b'. qq:a b: is the same as "a b", but qw:a b: becomes ("a", "b"). We always called it "quote word".
In all cases, using bracket delimiters (), [], {}, will match the opening and closing versions.
In Ruby you can use ' or " for strings in code.
And sometimes you see things like:
Basically anything following the %q becomes the delimiter for the string.
Also you can use % for strings in specific without needing hte q. It is the default delimiter option.
There is another interesting delimiter: %w
%s can be used to make a symbol.
creates :meh as a symbol.
Usefully for regular expression patterns you can do %r
And finally %e can be used to execute a shell command.
Also, there's probably more I don't know about. :P