[FAQ] [Revised] What do some of Ruby's symbols mean?

Thanks for the instant feedback. And apologies for the offensive late-night
manager humour. If only I’d limited it to my workplace… . Aside: I actually
find it difficult to come up with an example for the ternary operator, as I
don’t consider it “the Ruby way”.

Here’s the revision.

···

Q: What do some of Ruby’s symbols mean?

A:

This question is about non-aplhanumeric characters in Ruby code, not about
symbols like :name. For those, see [appropriate link]. Actual explanations
of the concepts behind them is beyond the scope of this answer.

<= < != == > >>
Relational operators, as per most other computer languages. Are often
redefined for use with various classes.

      • / %
        Common arithmetic operators. Some are defined also for String, Array
        and others.

===
Case comparison operator. Used to select branches in case statements.

<
Class inheritance, as in
class Car < Vehicle
end

<<
Singleton definition. See [appropriate link] for more info.
Also commonly used as a method for appending (e.g. Array and String).

… …
Range definers, as in 0…10 and “a”…“z”

:
Symbol definer, as in :name, where :name.to_s == “name”. See [a/l].
See next item also.

?:
Ternary decision operator, as in C. For example:
return (level == DEBUG) ? “” : detailed_info

{} =>
Hash definition, as in
numstr = { 1 => “one”, 2 => “two”, … }

[]
Array definition, as in
ndays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]

%w[]
String-Array converter, as in
dayabbrev = %w[Sun Mon Tue Wed Thu Fri Sat]
dayabbrev == [“Sun”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”]

//
Regular expression definition, as in
date_re = /(\d\d\d\d)-(\d\d)-(\d\d)/
y, m, d = date_re.match(“2002-12-01”)[1…3]

?!
Suffixes for methods (optional, of course). In general:
method! changes the object (e.g. String.gsub!)
method? returns boolean (e.g. Array.empty?)

Looking good. Some suggestions and typofixes:

<= < != == > >>

That last should be >=

Relational operators, as per most other computer languages. Are often
redefined for use with various classes.

      • / %
        Common arithmetic operators. Some are defined also for String, Array
        and others.

Might be worth defining % as it’s not standart

===
Case comparison operator. Used to select branches in case statements.

When you say “case a; when b”, a === b is implicitly called

<<
Singleton definition. See [appropriate link] for more info.
Also commonly used as a method for appending (e.g. Array and String).

Singleton class definition

. …

…, not .

Range definers, as in 0…10 and “a”…“z”

?!

Write ? ! instead of ?!, since we have two-character operators

This might also be a good place to explain the class#method notation,
#{} in strings, and the ?c to get a character’s ascii value, and perhaps
@ and @@ for completeness.

martin

···

Gavin Sinclair gsinclair@soyabean.com.au wrote:

Thanks again for even more feedback. I’ll take all that on board and post a
new revision, hopefully by the time you Americans wake up in the morning. It’s
going to be a big job, though!

Cheers,
Gavin

···

----- Original Message -----
From: “Gavin Sinclair” gsinclair@soyabean.com.au

Thanks for the instant feedback. […]

This still seems confusing, as I would expect detailed information to be
returned in debug mode.

···

On Sun, 2002-12-01 at 10:01, Gavin Sinclair wrote:

Thanks for the instant feedback. And apologies for the offensive late-night
manager humour. If only I’d limited it to my workplace… . […]

?:
Ternary decision operator, as in C. For example:
return (level == DEBUG) ? “” : detailed_info


– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich

“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)

When you say “case a; when b”, a === b is implicitly called

···

----- Original Message -----
From: “Martin DeMello” martindemello@yahoo.com


Are you sure? I thought b.===(a) was called. They are switched, which is
very cool.

Chris

Thanks again for even more feedback. I’ll take all that on board and post
a
new revision, hopefully by the time you Americans wake up in the morning.
It’s
going to be a big job, though!

To avoid confusion with Symbols, you might call these
punctuation marks or non-alphanumerics or something.

Just a thought.

Hal

···

----- Original Message -----
From: “Gavin Sinclair” gsinclair@soyabean.com.au
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Sunday, December 01, 2002 7:50 PM
Subject: Re: [FAQ] [Revised] What do some of Ruby’s symbols mean?

Thanks for the instant feedback. And apologies for the offensive
late-night
manager humour. If only I’d limited it to my workplace… . […]

?:
Ternary decision operator, as in C. For example:
return (level == DEBUG) ? “” : detailed_info

This still seems confusing, as I would expect detailed information to be
returned in debug mode.

Whoops, my mistake. It’s a stupid example anyway. Can anyone think of a
simple real-life example of using the ternary operator!?

Gavin

···

----- Original Message -----
From: “Jim Weirich” jweirich@one.net

On Sun, 2002-12-01 at 10:01, Gavin Sinclair wrote:

Oops - just checked the pickaxe, and you’re perfectly right of course.

martin

···

Chris nemo@hellotree.com wrote:

----- Original Message -----
From: “Martin DeMello” martindemello@yahoo.com

When you say “case a; when b”, a === b is implicitly called

Are you sure? I thought b.===(a) was called. They are switched, which is
very cool.

thing ? thing.to_s : ‘Thing does not exist’

Tim Bates

···

On Mon, 2 Dec 2002 05:32 pm, Gavin Sinclair wrote:

Whoops, my mistake. It’s a stupid example anyway. Can anyone think of a
simple real-life example of using the ternary operator!?


tim@bates.id.au

(exist? filename) ? open(filename) :
open(default_filename)

···

— Gavin Sinclair gsinclair@soyabean.com.au wrote:

----- Original Message -----
From: “Jim Weirich” jweirich@one.net

On Sun, 2002-12-01 at 10:01, Gavin Sinclair wrote:

Thanks for the instant feedback. And apologies
for the offensive
late-night
manager humour. If only I’d limited it to my
workplace… . […]

?:
Ternary decision operator, as in C. For
example:
return (level == DEBUG) ? “” :
detailed_info

This still seems confusing, as I would expect
detailed information to be
returned in debug mode.

Whoops, my mistake. It’s a stupid example anyway.
Can anyone think of a
simple real-life example of using the ternary
operator!?

Gavin

Whoops, my mistake. It’s a stupid example anyway. Can anyone think of a
simple real-life example of using the ternary operator!?

thing ? thing.to_s : ‘Thing does not exist’

Tim Bates

That’s great; thanks.

Gavin

···

From: “Tim Bates” tim@bates.id.au

On Mon, 2 Dec 2002 05:32 pm, Gavin Sinclair wrote: