The beauty of Ruby’s array subtraction operator

Today I had to a set of email addresses, one per line, from which I had to remove the addresses of folks that said “Don’t email me.” Those emails were in a separate file, one address per line.

I figured I’d have to do this again, so I wrote a ruby script to automate it. Below, stripper.rb

#put each address in an array, remove whitespace and make it all lowercase
 potential_emails = IO.readlines("potentials.txt").map! {|email| email.strip.downcase}
 delete_emails = IO.readlines("donotemail.txt").map! {|email| email.strip.downcase}

#use the beauty of ruby's array subtraction operator
 puts potential_emails - delete_emails

Simple, terse and readable.  Lovely!

But wait, there's more

2 thoughts on “The beauty of Ruby’s array subtraction operator

  1. You can eventually write ANYTHING in one line of Perl.

    You could definitely make this one line as well. The arrays potential_emails and delete_emails are there for readability.

    it coulda been
    puts IO.readLines(”potentials.txt”).map! {|email| email.strip.downcase} – IO.readlines(”donotemail.txt”).map! {|email| email.strip.downcase}

    How’s that for terse?

Leave a Reply

Your email address will not be published. Required fields are marked *