From Solid Foundation Web Development: Basic operations for arrays in Ruby
I have a bunch of files in a directory, and I want to delete all that begin their filename with the letters X16 (e.g. X16data.xml)
Dir.glob to select the files and iterated over what comes up in the pattern, using
File.delete to get rid of what I don't want (Thanks, Stack Overflow):
Dir.glob("X16*") do |file| File.delete(file) end
You can put any kind of regex in here, and it'll probably work. That's the theory anyway.
On my current project, I am trying to use
using rubyzip to unzip an archive. So far it's not working, and I'll probably shell out to Bash and Linux/Unix's
unzip to get it done.
I figured it out. Now I have to manage the unzipped files (deleting the unused, renaming the good, then deleting the good at the beginning of the run) and account for NOT running the program if there is no file on the other end.
While it calls itself out as old and out of date, I really like The Bastards Book of Ruby.
I recognize that Ruby is no longer the new hotness, but it's still so useful and, dare I say, user-friendly. For those reasons, I'd love to see updated versions of just about every book out there.
I'm using the old (as the hills) "Learning Ruby" by Michael Fitzgerald (2007, O'Reilly), The Pickaxe book ("Programming Ruby") from Ruby version 1.9.2 (2010/11, Pragmatic Programmers, though do I realize there is a 2013 edition).
The beginners books seem to be the oldest. At my level, everything seems to be working, so I will maybe complain a little less.
I do have a Rails book, "Rails Crash Course," by Anthony Lewis, that's much newer, but I'm not there quite yet. And there's always Michael Hartl's "The Ruby on Rails Tutorial", of which the more I see, the more I like.
I tend to learn things in programming when I have a problem to solve. This is just such a case.
I was working with a huge XML file, and I needed to trim elements out of it that begin with
<generic tag> and end with
</generic tag>, and include a random amount of text and other tags, across multiple lines, in between.
At first I tried using the Nokogiri gem, but it just wasn't happening. I was working on my Election Results script, and ... the election -- they hold it on a certain date, you know.
I would have to brute-force it. Like I always do.
My whole idea this cycle was to dump my giant
sed hack from elections past and use mostly (if not all) Ruby to parse the XML I get from the state of California and provide the JSON output my fellow dev needed for the front end. (I also have a ton of fixed-width ASCII from Los Angeles County to deal with, as well as scraped HTML from San Bernardino County, but those are other tales for other times.)
With the state data, I had the XML-to-JSON conversion covered with Ruby's Crack gem. But I just couldn't pare down the XML to make the JSON a manageable size.
A company called Prograils offers a great tutorial, Loops in Ruby - all possible approaches.
It looks like a good reference for when you're writing a Ruby program and need to figure out which kind of loop will work best in a particular situation.
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.
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
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.
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'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>"