Ruby quick reference arranged in ASCII sequence?

As a ruby beginner, I would be grateful for any links to a ruby
reference that is arranged in ASCII sequence.

I’ve seen many good tutorials that will teach me ruby from beginning to
end. But I’m a weekend (or month-end) programmer who wants to learn a
language just to do bits and pieces for myself. I don’t want to read 600
pages to find out what “?” is used for.

I look online for a piece of code that will already do 75% of what I’m
looking for - and then modify it. (I think this is called “not
re-inventing the wheel”, AKA "re-usable code).

For example, I wanted a piece of code that would read and write CSV
files. I found a good link. Within this link, there is the following
line:

last_name.length==0 ? printf(",") : printf(""%s",", last_name)

For this line, I would like to find a reference that explains the
following items in ASCII sequence:

?

%s
:

printf

Probably all of these items would be explained in a good tutorial, but
it might take hours to find them.

Hi,

I have no idea what you mean by “ASCII sequence” in this context.

The “reusable code” you talk about is called library. There are many CSV
libraries for Ruby, including the one in Ruby’s standard library:

By the way, that code you’ve found is not a good way of creating CSVs.

What Jan said.

But, if you do need a reference:

– Matma R.

Oh, you mean a reference that lists Ruby tokens in “alphabetical” order?

The book “The Ruby programming language” has such a list, but I don’t
know an online reference that does. For methods/operators and classes,
you can use the Ruby documentation itself:

http://ruby-doc.org/core-1.9.3/

Thanks for your kind response.

I have no idea what you mean by “ASCII sequence” in this context.

The following link is a good illustration:

http://www.simotime.com/asc2ebc1.htm#D160191

So, if you look at the tokens in my original post, and compare them with
the corresponding entries in the “ASCII” column in this link, you can
see that they are arranged in “ASCII sequence” in this context:

?

%s
:

printf

The “reusable code” you talk about is called library.

No. In the context of my post, “re-usable code” means googling for
someone else’s code and re-using it :slight_smile:

By the way, that code you’ve found is not a good way of creating CSVs.

That’s OK.

If the first re-usable code that I find does the job, then “the first
shall be last”.

Jan E. wrote in post #1089191:

Hi,

I have no idea what you mean by “ASCII sequence” in this context.

The “reusable code” you talk about is called library. There are many CSV
libraries for Ruby, including the one in Ruby’s standard library:

Class: CSV (Ruby 1.9.3)

By the way, that code you’ve found is not a good way of creating CSVs.

On Sat, Dec 15, 2012 at 7:00 AM, Old G. [email protected]
wrote:

As a ruby beginner, I would be grateful for any links to a ruby
reference that is arranged in ASCII sequence.
[…]
Probably all of these items would be explained in a good tutorial, but
it might take hours to find them.

You might try http://symbolhound.com to search for non-alphanumeric
things like those, along with the word Ruby to narrow things down.

Subject: Re: Ruby quick reference arranged in ASCII sequence?
Date: Sun 16 Dec 12 12:04:23AM +0900

Quoting Old G. ([email protected]):

No. In the context of my post, “re-usable code” means googling for
someone else’s code and re-using it :slight_smile:

If you limit yourself to that, you will never learn to code.

Nothing inherently evil with that, of course.

Carlo

On Sat, Dec 15, 2012 at 5:40 PM, tamouse mailing lists
[email protected] wrote:

==

the result of expr2, otherwise it gets the result of expr3. If you

last_name.empty?
last_name.empty?

however, it’s a gross misunderstanding of the printf method. printf
So all in all, what this

examples that might do what you want, and stop looking the moment you
get close, just like your bookshelves that are all wobbly and falling
over because you stopped looking for how to build them when you found
a hammer and nails, your code will rot and fall over as well. And that
might be okay.

Crikey, I forgot to explain the last bit:

the #{expr} notation inside a double quoted string means to substitute
the value of the expression at that point in the string, also known as
“interpolation”. So:

variable = "something"
"this string includes the value of #{variable}"

would yield

"this string includes the value of something"

You can put any valid ruby expression inside the braces:

"this string includes the value of #{10 * 1024 / 3.1415}"

would yield

"this string includes the value of 3259.5893681362404"

On Sat, Dec 15, 2012 at 9:26 AM, Eric C.
[email protected] wrote:

:

printf

Probably all of these items would be explained in a good tutorial, but
it might take hours to find them.

You might try http://symbolhound.com to search for non-alphanumeric
things like those, along with the word Ruby to narrow things down.

I don’t know where you’d find such a reference, but you could build
one as you go.

In the meantime, I know you asked for a reference, but the thing
you’re wondering about is this:

var = expr1 ? expr2 : expr3

That’s known as the “ternary” operator. if expr 1 is true, var gets
the result of expr2, otherwise it gets the result of expr3. If you
unpack this into more typical syntax, you get:

if expr1
  var = expr2
else
  var = expr3
end

The second form is much clearer when reading code, but the first form
saves quite a bit of space and typing.

The next aspect is to look at the printf statements. These are
actually a really non-ruby way of doing something, this looks a lot
more like C, C++, or php.

In ruby, everything (ALL THE THINGS!!) are objects. Objects are told
what to do with their data by methods.

last_name.length==0 ? printf(“,”) : printf(“"%s",”, last_name)

So lets break this down.

last_name.length --- this is telling the object last_name to

return it’s length

== --- this is the equals comparison

so
last_name.length == 0

is asking if last_name is empty. Ruby has a better way of writing this:

last_name.empty?

The empty? method simply asks exactly what the previous way was
attempting to find out. empty? is implemented something like this:

def empty?
  self.length == 0
end

So why do that? Because this:

last_name.empty?

is more readable than the other. It’s a direct expression of what you
want to know at that point.

Now, onwards

printf(",")

This starts to get into error territory. Just using this as it is is
incorrect. Not that it would produce a run-time error – it won’t –
however, it’s a gross misunderstanding of the printf method. printf
means “Print with formatting”. If you aren’t formatting anything,
don’t. Use print instead.

The next part

printf("\"%s\","," last_name)

is a slightly more legitimate use of printf, in that the variable
last_name is being formatted as a quoted sting and printed.

So all in all, what this

last_name.length==0 ? printf(",") : printf("\"%s\",", last_name)

means, in pseudo code, is:

If the last name is empty,
just print a comma
Otherwise,
print the last name surrounded by double quotes
followed by a comma

Now, that will all work. But it isn’t idiomatic ruby.

print last_name.empty ? "," : "\"#{last_name}\","

makes better use of ruby’s expressiveness.

Why learn idioms, you may ask?

Simple, so you will understand the other speakers of the language you
are trying to learn. If you write code solely by casting about for
examples that might do what you want, and stop looking the moment you
get close, just like your bookshelves that are all wobbly and falling
over because you stopped looking for how to build them when you found
a hammer and nails, your code will rot and fall over as well. And that
might be okay.

On Sat, Dec 15, 2012 at 5:32 PM, 7stud – [email protected] wrote:

comma = “,”

I cannot fathom why this line would ever appear in any program.

tamouse mailing lists wrote in post #1089227:

On Sat, Dec 15, 2012 at 9:26 AM, Eric C.

Now, that will all work. But it isn’t idiomatic ruby.

print last_name.empty ? "," : "\"#{last_name}\","

makes better use of ruby’s expressiveness.

You forgot a question mark in there:

print last_name.empty?  ? "," : "\"#{last_name}\","

Personally, I would never write a ternary statement that was so
confusing. Terseness should never be favored over clarity. And Ruby
has six different quoting mechanisms for strings, so resorting to
escaping quote marks inside strings isn’t necessary.

last_name = “Smith”
comma = “,”
quoted_last_name = %Q{"#{last_name}"}

puts last_name.empty? ? comma : quoted_last_name

On Sat, Dec 15, 2012 at 7:32 PM, 7stud – [email protected] wrote:

You forgot a question mark there:

print last_name.empty?  ? "," : "\"#{last_name}\","

Yup. This is one of the things a second pair of eyes is great for as
well. Typing just off the top of my head. If I’d done it in irb, the
syntax would have blown and I’d have noticed it. It’s the tough ones
that second eyes helps on a lot.

puts last_name.empty? ? comma : quoted_last_name

I always seem to forget about the % quote operators, even though I’m
used to the similar thing in perl. Someone was recently asking “why
would you use them” and here is the perfect example.

Given the OP’s code had a comma at the end wondering if there’s an
even better way.

Assuming a situation where output is accumulating via printf
statements in the OP code, no line terminator (as puts will include),
say collecting a set of names to print in a line, separated by commas,
say from a hash for lack of better choice at the moment:

starring = [{:first_name => ‘Sammy’, :last_name => ‘Hagar’},
{:first_name => ‘Billy’, :last_name => ‘The Kid’},
{:first_name => ‘Madonna’, :last_name => ‘’},
{:first_name => ‘Bono’, :last_name => ‘’}]

=> [{:first_name=>“Sammy”, :last_name=>“Hagar”},

{:first_name=>“Billy”, :last_name=>“The Kid”},
{:first_name=>“Madonna”, :last_name=>“”}, {:first_name=>“Bono”,
:last_name=>“”}]

starring.collect{|star|
%Q{#{person[:first_name]}#{person[:last_name].empty? ? ‘’ : %Q{
“#{person[:last_name]}”}}}}.join(', ')

=> “Sammy "Hagar", Billy "The Kid", Madonna, Bono”

when printed, yields:

Sammy “Hagar”, Billy “The Kid”, Madonna, Bono

making it a list with/without the oxford comma is for another day.

On 16 December 2012 12:03, Yossef M. [email protected] wrote:

On Sat, Dec 15, 2012 at 5:32 PM, 7stud – [email protected] wrote:

comma = “,”

I cannot fathom why this line would ever appear in any program.

For when the grammar changes, of course. One day, not too far away,
we’ll
be writing ‘:bangbang:’ (U+203C) instead of commas.


Matthew K., B.Sc (CompSci) (Hons)
http://matthew.kerwin.net.au/
ABN: 59-013-727-651

“You’ll never find a programming language that frees
you from the burden of clarifying your ideas.” - xkcd

On Sat, Dec 15, 2012 at 8:03 PM, Yossef M. [email protected]
wrote:

On Sat, Dec 15, 2012 at 5:32 PM, 7stud – [email protected] wrote:

comma = “,”

I cannot fathom why this line would ever appear in any program.


-yossef

And this is the beauty of the language. It’s expressiveness is
wonderful.

You might consider that a waste of effort. Some people have a hard
time distinguishing between , and . on the screen.

Am 19.12.2012 15:05, schrieb Old G.:

Thanks for all the comments. The generosity of the folks on this forum
is amazing.

As a bonus, there seems to have been a lively discussion about “best”
code.

I fully support the honest attempts by developers to write the best
code. But in my case, I simply want to write quick one-off programs that
do the job, and might never be used again. I use whichever language

Pretty often they do tend to being used again…

At that point, I hadn’t written a line of java in my life. So I
contacted several java forums and asked for advice. Most replies
recommended me to read 3 java books, attend 2 courses, and get a java
qualification to at least Nobel Peace Prize level. None of the replies
gave me a list of the steps that I needed to take to save the cube.

So I contacted the developer. He advised me to change the line:

[…]

I don’t understand any of it. I still don’t know what “Serializable”
means. (I feel quite guilty about that.) But it works great.

Kind of sad :frowning:

Reminds me of a child that’s just begun to learn reading
and questions (or has to question) his parents about every
street sign or billboard on the way to school.

Only the child eventually will be able to read on his own…

of “?” and “:”

But about half of ruby syntax uses “:” so that threw me off. (There are
186 occurrences of “:” in the zenspider reference.)

They are used to signify symbols.

I’ve just found out that almost all of the non-alphanumeric characters
in ruby are in the first three pages (yes, three double-column pages) of
the index for “Perl in a Nutshell”. That’s convenient. I can find

But Perl is not Ruby!

alphabetic references by googling. It’s not so easy to google for “@”,
or “::”, or “~”. But Eric C.'s link to symbolhound looks
very useful.

Reading up on the basics would be a better investment than
wasting time with googling.

Thanks for all the comments. The generosity of the folks on this forum
is amazing.

As a bonus, there seems to have been a lively discussion about “best”
code.

I fully support the honest attempts by developers to write the best
code. But in my case, I simply want to write quick one-off programs that
do the job, and might never be used again. I use whichever language
is found by google, including languages that I know nothing
about.

Here’s an example. There’s a Rubik’s cube on this site:

It’s an applet, so nothing can be saved. I wanted to be able to save the
“last good position” before I mess everything up. Or to simply save a
position and return to it some other day.

At that point, I hadn’t written a line of java in my life. So I
contacted several java forums and asked for advice. Most replies
recommended me to read 3 java books, attend 2 courses, and get a java
qualification to at least Nobel Peace Prize level. None of the replies
gave me a list of the steps that I needed to take to save the cube.

So I contacted the developer. He advised me to change the line:

public class RubiksCubeApplet extends Applet

to

public class RubiksCube extends Panel implements Serializable

I created “Save” and “Restore” buttons by copying his code for the
“Scramble” and “Give up” buttons. There’s no “Close” button because I
simply click Ctrl+C in the console.

I don’t understand any of it. I still don’t know what “Serializable”
means. (I feel quite guilty about that.) But it works great.

My code is so ugly that the only people who are allowed to see it are
young family members and friends who are writing their first programs.
The big reward for me is when they come back later and show me how I
should have written the code :slight_smile:

Regarding my original post, the zenspider reference quoted by Bartosz
Dziewoński is excellent. That’s how I found out what “?” means. It’s
just a C conditional statement.

In any language other than ruby, I might have recognized the combination
of “?” and “:”

But about half of ruby syntax uses “:” so that threw me off. (There are
186 occurrences of “:” in the zenspider reference.)

I’ve just found out that almost all of the non-alphanumeric characters
in ruby are in the first three pages (yes, three double-column pages) of
the index for “Perl in a Nutshell”. That’s convenient. I can find
alphabetic references by googling. It’s not so easy to google for “@”,
or “::”, or “~”. But Eric C.'s link to symbolhound looks
very useful.

Many thanks for all the help, including those whose name I might not
have mentioned :slight_smile:

On Sat, Dec 22, 2012 at 3:32 PM, [email protected] wrote:

Reading up on the basics would be a better investment than
wasting time with googling.

Meh, It’s their time to waste, use how they see fit, do what they want.