# Symbol confusions!

**URL:** <https://www.ruby-forum.com/t/symbol-confusions/151068>\
**Category:** Ruby\
**Created:** [October 16, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/symbol-confusions/151068 "2008-10-16T14:08:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 16, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/1 "2008-10-16T14:08:09Z")

</div>

Hi, I’m new to ruby and I’m going thru pretty good as I’ve done java but  
the only thing, at this point, thats really bothering me is the use of  
symbols like:  
%w{}  
%r{}  
%7d etc etc…

So, is there anywhere I can find good discussions about such symbols coz  
until now I’m using it blindly… thanks

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 16, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/2 "2008-10-16T14:45:11Z")

</div>

On Oct 16, 2008, at 8:07 AM, Jay P. wrote:

> until now I’m using it blindly… thanks  
> Do you have a book like the Pickaxe?[1] That will go into great  
> detail on all the features that Ruby has to offer.

Briefly, however:

%w{} is a way to turn a list of words into an array:  
%w{ one fish two fish }  
=\> [“one”, “fish”, “two”, “fish”]

%r{} is a regular expression literal. %r{stuff._} is like /stuff._/  
without having to quote slashes (which is the main reason to see it  
used if you’re matching a file path, for example)

%7d is probably a format specification from Kernel#sprintf or String#%  
meaning a decimal value in a minimum of 7 columns:  
“%7d”%42  
=\> " 42"

Oh, and “Kernel#sprintf” (generally, ClassName#method) means the  
instance method ‘sprintf’ from the class (or module) ‘Kernel’ (so, the  
‘%’ method from the String class).

-Rob

[1] [http://www.pragprog.com/titles/ruby/programming-ruby](http://www.pragprog.com/titles/ruby/programming-ruby)

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 16, 2008, 3:47pm UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/3 "2008-10-16T15:47:40Z")

</div>

Jay P. wrote:

> Thanks Rob you made my life lot easier.. no I don’t have that book I  
> just have ‘Agile Web D. with Rails 2nd Edt.’ so, ruby is just a  
> small part here and hence internet has been my only source.

The old, first edition version of the PragProg Programming Ruby book  
(for ruby-1.6) is available for free online:

> **[Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/)**

The chapter entitled “The Ruby Language” goes into the constructs like  
%w, %r, %q etc (near the top; scroll down to “General Delimited Input”,  
or click “General Delimited Input” in the index on the left hand side)

> Also I confusion about the use of slashes:  
> %r{(\d{1,2}):(\d{1,2}):(\d{4})}  
> whats the use of forward slash here  
> and the ‘+’ sign below:  
> split(/[^0-9]+/)

Scroll down to “regular expression patterns” for the answer to these.

Hopefully this will convince you to buy the 2nd edition of Programming  
Ruby. Avoid the 3rd unless you’re experimenting with ruby 1.9.

I have only two Ruby books: this one, plus Agile Web D. with  
Rails. They are both superb.

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 16, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/4 "2008-10-16T15:31:38Z")

</div>

Rob B. wrote:

> Do you have a book like the Pickaxe?[1] That will go into great  
> detail on all the features that Ruby has to offer.
> 
> Briefly, however:
> 
> %w{} is a way to turn a list of words into an array:  
> %w{ one fish two fish }  
> =\> [“one”, “fish”, “two”, “fish”]
> 
> %r{} is a regular expression literal. %r{stuff._} is like /stuff._/  
> without having to quote slashes (which is the main reason to see it  
> used if you’re matching a file path, for example)
> 
> %7d is probably a format specification from Kernel#sprintf or String#%  
> meaning a decimal value in a minimum of 7 columns:  
> “%7d”%42  
> =\> " 42"
> 
> Oh, and “Kernel#sprintf” (generally, ClassName#method) means the  
> instance method ‘sprintf’ from the class (or module) ‘Kernel’ (so, the  
> ‘%’ method from the String class).
> 
> -Rob
> 
> [1] [http://www.pragprog.com/titles/ruby/programming-ruby](http://www.pragprog.com/titles/ruby/programming-ruby)
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks Rob you made my life lot easier.. no I don’t have that book I  
just have ‘Agile Web D. with Rails 2nd Edt.’ so, ruby is just a  
small part here and hence internet has been my only source.  
Also I confusion about the use of slashes:  
%r{(\d{1,2}):(\d{1,2}):(\d{4})}  
whats the use of forward slash here  
and the ‘+’ sign below:  
split(/[^0-9]+/)  
Also I think I’ve seen the use of other words with ‘%’ like %w but I  
don’t remember which words in particular.

Thanks.

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 16, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/5 "2008-10-16T15:56:53Z")

</div>

Le 16 octobre à 14:07, Jay P. a écrit :

> So, is there anywhere I can find good discussions about such symbols coz  
> until now I’m using it blindly… thanks

An excellent quick resource for Ruby can be found here :

> **[Ruby QuickRef | zenspider.com | by ryan davis](https://zenspider.com/ruby/quickref.html)**
>
> Ruby quick reference.

The use of % constructs is covered in the types section (in the strings,  
arrays and regexen).

Your latter question about // is also covered - those are regular  
expressions. A very quick reference is also included.

BTW, the best way to learn ruby itself (without rails) is to log on a  
machine where it is installed and launch the irb console program. It  
allows you to test snippets of code interactively :

15:41 fred@grappa:~\> irb

> > %w(a b c d e f)  
> > =\> [“a”, “b”, “c”, “d”, “e”, “f”]  
> > %w(a b c d e f).is\_a? Array  
> > =\> true

(What I type is after the \>\> prompt, the =\> is the response, i.e. what  
the expression evaluates to.)

G’d luck and enjoy !

Fred

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 16, 2008, 4:50pm UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/6 "2008-10-16T16:50:59Z")

</div>

On 16 Oct 2008, at 14:31, Jay P. wrote:

> > 
> 
> Thanks Rob you made my life lot easier.. no I don’t have that book I  
> just have ‘Agile Web D. with Rails 2nd Edt.’ so, ruby is  
> just a  
> small part here and hence internet has been my only source.  
> The first edition of the pickaxe is available online  
> [http://www.rubycentral.com/book/](http://www.rubycentral.com/book/)  
> A little out of date (it covers ruby 1.6) but there’s plenty of useful  
> stuff in there

There’s also the humble little ruby book:

> **[Site not found · DreamHost](http://www.humblelittlerubybook.com/)**
>
> The owner of this domain has not yet uploaded their website.

which is a free download

> Also I confusion about the use of slashes:  
> %r{(\d{1,2}):(\d{1,2}):(\d{4})}  
> whats the use of forward slash here  
> and the ‘+’ sign below:  
> split(/[^0-9]+/)  
> / can be used to delimit a regular expression. In a regular expression

- denotes “one or more times”)

> Also I think I’ve seen the use of other words with ‘%’ like %w but I

%r (alternative way of writing regular expressions), %x (like `), %q  
and %Q (like ’ and " respectively) and %w are the common ones

Fred

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 16, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/7 "2008-10-16T16:46:17Z")

</div>

On Oct 16, 2008, at 9:31 AM, Jay P. wrote:

> > Oh, and “Kernel#sprintf” (generally, ClassName#method) means the  
> > Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> > split(/[^0-9]+/)  
> > Also I think I’ve seen the use of other words with ‘%’ like %w but I  
> > don’t remember which words in particular.
> 
> Thanks.

Brian’s and Fred’s responses should help, too (esp. the link to the  
earlier Pickaxe)

The \d is an escape within a regular expression that’s equivalent to  
[0-9] and + is like {1,} in regular expressions to match “one or more  
of…”. x+ is xx\* or x{1,} /[^0-9]+/ is a sequence of one or more  
non-digits.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 17, 2008, 1:14pm UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/8 "2008-10-17T13:14:54Z")

</div>

wow!! thanks guys… Now things’ve already started making sense…

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 19, 2008, 10:39am UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/9 "2008-10-19T10:39:16Z")

</div>

2008/10/17 Jay P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ## wow!! thanks guys.. Now things’ve already started making sense..
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Threre is also %s{..}:

%q{abc def} #=\> String “abc def”  
%s{abc def} #=\> Symbol :“abc def”

Just my 2 cents 🙂

Artem

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 16, 2008, 7:15pm UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/10 "2008-10-16T19:15:01Z")

</div>

Jay P. wrote:

> Hi, I’m new to ruby and I’m going thru pretty good as I’ve done java but  
> the only thing, at this point, thats really bothering me is the use of  
> symbols like:  
> %w{}  
> %r{}  
> %7d etc etc..

Questions like these are understandable because google does not appear  
to index ‘%’. In this case googling for “ruby cheat sheet” is a good  
first step.

---

<div class="post-metadata">

**Author:** ![jicksta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ba0ec/32.png) [@jicksta](https://www.ruby-forum.com/u/jicksta)\
**Post date:** [October 20, 2008, 6:58am UTC](https://www.ruby-forum.com/t/symbol-confusions/151068/11 "2008-10-20T06:58:50Z")

</div>

Just for fun, you can also use any delimiter you want in place of {}.

> > %w( see me run )  
> > =\> [“see”, “me”, “run”]

> > %q/ fun time /  
> > =\> " fun time "

> > %w\* fun time! \*  
> > =\> [“fun”, “time!”]

> > %r# ha #  
> > =\> / ha /

–Jeremy

On Sun, Oct 19, 2008 at 3:37 AM, Artem V.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> %s{abc def} #=\> Symbol :“abc def”
> 
> Just my 2 cents 🙂
> 
> Artem

–

> **[Jeremy McAnally](http://jeremymcanally.com/)**

> **[entp - web incubator and consultancy](http://entp.com/)**

> **[OMG GIFs - Download & Share | GIFDB.com](https://gifdb.com/omg)**
>
> Browse 226 free omg GIFs. Download and share them on GIFDB.com — works in WhatsApp, Telegram, Discord, X and more.

My books:

> **[Ruby in Practice - Jeremy McAnally and Assaf Arkin](https://www.manning.com/books/ruby-in-practice)**
>
> Like Ruby itself, Ruby in Practice will make you more productive. The book shows you practical techniques and strategies for small projects and large-scale…

[http://humblelittlerubybook.com/](http://humblelittlerubybook.com/) (FREE!)
