16 Jul, 2009, Runter wrote in the 1st comment:
Votes: 0
So I had trouble finding this information and thought I'd post it.
In Ruby you can use ' or " for strings in code.

puts "a test string"
puts 'a test string'

puts ' "quotes included" '
puts " 'quotes included' "


And sometimes you see things like:
%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..
name = "Bob"
str1 = "Hello #{name}!"
str2 = 'Hello #{name}!'
puts str1 # Hello Bob!
puts str2 # Hello #{name}!
17 Jul, 2009, kiasyn wrote in the 3rd comment:
Votes: 0
Runter said:
# %s(meh)


Can you do like

bla = 'abc'
bla_sym = %s(bla)

etc?
17 Jul, 2009, Runter wrote in the 4th comment:
Votes: 0
kiasyn said:
Runter said:
# %s(meh)


Can you do like

bla = 'abc'
bla_sym = %s(bla)

etc?


bla.to_sym is something like that. :grinning:
17 Jul, 2009, quixadhal wrote in the 5th comment:
Votes: 0
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:
Votes: 0
I believe so, unless I am misunderstanding.

%w[hello there bob] # ['hello','there','bob']

Is that what you mean? =)
17 Jul, 2009, quixadhal wrote in the 8th comment:
Votes: 0
Yep, that looks right. Cool. :)

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.
0.0/8