What does the construct "?/" refer to in Ruby?
January 14, 2008 2:09 PM   Subscribe

What does the construct "?/" refer to in Ruby?

The Pragmatic guide for TextMate includes an code fragment that has me stumped. It's essentially "if foo == ?/ then bar" What is the ?/, though?
irb(main):001:0> ?/
=> 47
irb(main):002:0> ?/.class
=> Fixnum
Google is being singularly unhelpful here, since it doesn't like to search for punctuation. The book's errata offers no further hints.
posted by nakedcodemonkey to Computers & Internet (6 answers total) 1 user marked this as a favorite
It's the ascii code for '/'.

Ruby syntax doc
posted by smackfu at 2:23 PM on January 14, 2008

Yup, it's shorthand for getting the bytecode for that character, primarily because:

"foo"[0] # => 102
?f # => 102
"foo"[0] == ?f # => true

In Ruby 1.9, ?f will return a string "f", as will "foo"[0], since String is now aware of charsets and encodings:

"foo"[0] # => "f"
?f # => "f"
"foo"[0] == ?f # => true

posted by Freaky at 3:13 PM on January 14, 2008

In more detail, the question mark before a bare character gives you the ascii value of that character. The fact you chose question mark just makes it look cooler than doing ?a, or ?z. Fire up irb and test it out.
posted by cschneid at 3:15 PM on January 14, 2008

Not that this helps answer the main question, but Google Code Search will search for code snippets. Here's a search that looks for "?/" in Ruby code spippets.
posted by philomathoholic at 3:17 PM on January 14, 2008

Thanks philomathaholic, that tip actually helps more than you think.
posted by nakedcodemonkey at 4:26 PM on January 14, 2008

Okay, now the code block is clear. It's searching for xml-ish elements by testing whether foo[1] is a forward slash. Thanks all!
posted by nakedcodemonkey at 4:39 PM on January 14, 2008

« Older Is my mom's behavior towards my little brother...   |   Make admissions go "wow" Newer »
This thread is closed to new comments.