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.