Title photo
frugal technology, simple living and guerrilla large-appliance repair
Tue, 14 Jun 2016

Quick Ruby: Open and write a file

Need to open an existing file, create a new one and write to that file in Ruby?

This Stack Overflow answer makes it super simple.

I already used this in one of my little Ruby programs.

In the same thread (but earlier), here's another one.

Sun, 12 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
Fri, 10 Jun 2016

Ruby for Admins

There's a great book available on the web, "Ruby for Admins."

Grammatically it's a little rough -- I would love to contribute via GitHub if that was available -- but the information seems solid.

Fri, 12 Sep 2014

Ruby's 'insert' method is something I'm definitely going to use

It's not a secret that I'm starting to look into the Ruby programming language. I've got a mess of second-hand books, plus there are plenty of helpful web sites.

I was looking into the gsub method of search/replacing in Ruby when I stumbled across something very useful: the insert method.

I'm sure there are plenty of better ways to do this, but the fact that I can do this and understand it ... that's something.

Here is what I'm talking about. I did it all in the interactive Ruby shell (aka irb) and have revised it because it's even easier to type out than I thought:

irb(main):017:0> phrase = "The quick brown fox jumped over the lazy dog's back"
=> "The quick brown fox jumped over the lazy dog's back"

irb(main):020:0> phrase.insert 0, "<bold>"
=> "<bold>The quick brown fox jumped over the lazy dog's back"

irb(main):024:0> phrase.insert -1, "</bold>"
=> "<bold>The quick brown fox jumped over the lazy dog's back</bold>"