[aklug] Re: perl lists question, sorting, sort of.

From: Christopher Howard <choward@indicium.us>
Date: Wed Aug 18 2010 - 15:40:46 AKDT

On 08/18/2010 09:46 AM, Arthur Corliss wrote:
> On Wed, 18 Aug 2010, Christopher Howard wrote:
>
>> Yeah sure, like so:
>>
>> ruby -e "puts 'hi'; puts 'there'"
>>
>> You /can/ use semicolons in ruby, you just don't /need/ to use them.
>>
>> Also, indentation in Ruby is irrelevant to the interpreter. But most
>> Ruby programmers still indent for readability.
>
> :-) Thanks for the info. I have obviously never slung a line of code in
> ruby. That said, *most* programmers indent for readability (though I don't
> see any point for a throw-away one-liner on the CLI), I just couldn't
> remember if the ruby crowd had adopted some of python's mandates.
>
> Another question: if you don't typically use semicolons then how do you
> split a single line of code across multiple lines? Playing with your source
> example it seems to generate syntax errors when I introduce newlines
> between
> some words. Seems there's limited intelligence in the ruby parser for these
> issues. Ironically, with semi-mandatory semicolons in Perl it allows the
> parser to be even less intelligent and still work. Your instruction
> boundaries are always clearly marked.
>
> --Arthur Corliss
> Live Free or Die

The parsing is fairly natural. Mostly you can go with what "looks
right." For example, you can do

puts "blah" +
   "blah" +
   "blah"

Instead of

puts "blah" + "blah" + "blah"

However, you can't split between a method and its first argument,
because Ruby won't know that the two are connected. (In Ruby, it is
valid for an object to sit on a line by itself.) For example:

puts
"blah"

Will just print a blank line, since no argument has been passed to puts.

In Ruby, arguments to a method can be either simply placed after the
method call or placed in parenthesis. So

puts "blah" +
   "blah" +
   "blah"

is the same as

puts("blah" +
   "blah" +
   "blah")

You should also be aware that ruby has blocks, and a special
construction for passing them in to functions. Blocks can be designated
by semicolons [{, }] or by the keywords "do" and "end". For example:

my_array = ["this", "that", "whatever"]
my_array.each do |array_element|
   puts "element content: " + array_element.to_s
end

"each" is a method of the Ruby Array "my_array", which takes the
following code block and uses it as an iterator function. "each"
successively executes the code in the code block, putting the value of
the element into the variable "array_element".

Anyway, I could go on and on about cool Ruby syntax, but let me know if
you have any more specific questions.

-- 
Christopher Howard
http://frigidcode.com
http://theologia.indicium.us
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Wed Aug 18 15:40:58 2010

This archive was generated by hypermail 2.1.8 : Wed Aug 18 2010 - 15:40:58 AKDT