Title photo
frugal technology, simple living and guerrilla large-appliance repair

Regular blog here, 'microblog' there

Many of my traditional blog post live on this site, but a great majority of my social-style posts can be found on my much-busier microbloging site at updates.passthejoe.net. It's busier because my BlogPoster "microblogging" script generates short, Twitter-style posts from the Linux or Windows (or anywhere you can run Ruby with too many Gems) command line, uploads them to the web server and send them out on my Twitter and Mastodon feeds.

I used to post to this blog via scripts and Unix/Linux utilities (curl and Unison) that helped me mirror the files locally and on the server. Since this site recently moved hosts, none of that is set up. I'm just using SFTP and SSH to write posts and manage the site.

Disqus comments are not live just yet because I'm not sure about what I'm going to do for the domain on this site. I'll probably restore the old domain at first just to have some continuity, but for now I like using the "free" domain from this site's new host, NearlyFreeSpeech.net.

Sat, 11 Jun 2016

Learning Ruby: Loops and arrays

This entry shows what I'm doing when I practice programming. I find it helpful to write little programs that use the concepts I'm trying to learn.

Why Ruby? you might ask. No particular reason. I've spend a semester learning C++, and I was pleasantly surprised to find out that the things I learned are applicable in many other programming languages. Ruby is one of them.

Writing scripting-type programs is one of the things I do. I have "practiced" recently with Bash (and all the little Unixy utilities that go along with it) and Perl (for the add-on that does the statistics for this blog).

Ruby just happens to interest me. I'm also interested in Elixir, but for the kind of things I'm interested in doing right now, Ruby with its many, many Gems and "make programmers happy" philosophy looks like a good fit.

I could say the same thing about Perl (or Python, or Java), but for now I'm playing around with Ruby.

I find that writing little programs like these is a great way to learn. And writing the "same" program in different languages is also very helpful. I could re-write this one in, say, Perl or JavaScript (using either Node in the console or rendering it on an HTML.

Here is the program I wrote to practice using Ruby loops and arrays. I also worked with strings (and converting arrays to strings and back again) and outputting results to the terminal.

What is an array? Here's what it is (in my mind anyway): A collection of pieces of data that can be manipulated as a whole. You can mix numbers and strings. In Ruby, arrays can even contain other arrays. I read that somewhere.

Here is the program:

#!/usr/bin/env ruby

=begin

The purpose of this program is to experiment
with Ruby loops, arrays, strings, integers and output.

The program creates an array, uses loops
to *push* numbers into that array, shows what
the array looks like at every stage using *puts*
and *inspect*, then uses *shift* to remove
numbers from the array, also showing
what it looks like at every stage.

There are two more loops in this script.
One prints out numbers ascending, the other
descending. 

=end

# Create an array

number_array = Array.new

# loop uses *push* to add a number,
# *puts and *inspect* to print it

(1..10).each do |i|
    number_array.push(i)
    puts number_array.inspect

end

# loop uses *shift* to remove
# an array element
# 1...10 with three dots runs
# the loop until 9, not 10

(1...10).each do |i|
    number_array.shift
    puts number_array.inspect
end

# *puts* on its own adds a blank line

puts

# *print* prints the output without adding
# a newline character (aka \n) like *puts*

for i in 1..10
    print i
    print " "
end

puts

10.downto(1).each do |i|
    print i
    print " "
end