[Tutor] beginning to code
Rick Johnson
rantingrickjohnson at gmail.com
Sun Sep 10 21:29:29 EDT 2017
On Sunday, September 10, 2017 at 1:14:40 PM UTC-5, leam hall wrote:
> I will add my +1 to the careful editing of code. Python's
> use of white space is pretty good once you get used to it.
Python's mandate that all blocks must use whitespace is by
far my favorite feature. A clean code structure is very
important to me, but consistency is most important of all.
> My Ruby code looks a lot like my Python code. :)
Nothing wrong with that! Ruby adopts the "Tim Toady"[1]
approach to coding, as opposed to Python's loosely followed:
"There should be one-- and preferably only one --obvious way
to do it." And while your Ruby code may look similar to
your Python code, if you examine random Ruby scripts in the
wild, you will find a wide variety of structures and
multiple forms of doing the same thing that on superficial
examination, at least, may not be readily apparent. For
instance, in Python, there is only one way to write a "for
loop":
for var on iterable:
# do something
However, Ruby allows multiple forms, the first of which
almost exactly mimics Python syntax (except for the `end`
keyword and the mising colon):
for var in iterable
# do something
end
But Ruby offers a second form using a method of the
"iterable object", with the block denoted by 'do' and `end`
keywords:
iterable.each do |var|
# do something
end
And futhermore, Ruby offers a third form using a method of
the "iterable object" (as before), except, this time, using
braces to denote the block:
iterable.each{|var|
# do something
}
Whew! Now how's that for a meet and greet with Tim Toady? As for
me, when i write a "for loop" in Ruby, i choose the second
form, because:
(1) I won't use braces to denote my blocks unless i can
write the entire construct on a single line, as in:
iterable.each{|var| # do something}
(2) When i use the first form (you know, the one that
resembles Python code), i sometimes become confused and
forget that i'm writing Ruby code altogether, and then i get
slammed with syntax errors later. Most of the time, it's
because i instinctively placed a colon at the end of the
first line of a "for loop" structure -- Damn you Python! :-)
At one point, i became so sick of repeating this simple
mistake, i was forced to write a script that would search
for misplaced colons and hilight them for me, which
unsurprisingly, greatly increased my workflow!
Ruby is a neat language, and while Python will probably
always be my favorite little scripting language, Ruby has
some advantages over Python.
============================================================
EXAMPLE 1:
============================================================
Firstly, method chaining in Ruby follows a naturally
intuitive left-to-right progression. Consider a contrived
example where we have an array of floats, and we need to
determine how many unique integers can be derived from
these floats.
Ruby:
farray = [1.5, 1.9, 2.0, 1.0]
uniqueIntegers = farray.map{|f| f.to_i()}.uniq.length
Python:
flist = [1.5, 1.9, 2.0, 1.0]
uniqueIntegers = len(set(map(lambda f:int(f), flist)))
Holy Buhjeebus! Was that Lisp or Python code? o_O
Obviously Ruby is superior in this test, as the python code
is nothing but an unintuitive and nested mess. "Flat is
better than nested"...
============================================================
EXAMPLE 2:
============================================================
(oops, looks like i'm being cut short. I'll get back to this
Pepsi challenge ASAP. So stay tuned...)
[1] https://en.wikipedia.org/wiki/There%27s_more_than_one_way_to_do_it
More information about the Python-list
mailing list