SoftOver
 
Recommended


User login


 

Ruby

   

Recently I had to find all IP addresses in the access log for the first 10 minutes after 1PM. Here is one-liner I used:

ruby -n -e '$_.scan(/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*2007:13:0/){ puts $1}' < access.log | sort -u

Please note that I did not care about IP address correctness, I'd use much longer regexp if I did:

/((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)).*2007:13:0/
    Sometimes I need to make a call that may fail. I do not want to give up immediately, but do not want to try forever neither. In this case a small try function comes in handy:
# Make +count+ attempts to yield a block
# ====Example:
#  def crazy_fun 
#    puts a=rand
#    raise if a<0.9 
#  end
#
#  try(5) {crazy_fun} rescue puts 'O-ops'
def try(count)
  begin
    yield
  rescue
    retry if (count-=1)>0
    raise
  end
end
    Recently I've stumbled upon an interesting code at the Ruby Quiz site.

Unnecessary details dropped, it looks like:

arr = File.open("/usr/share/dict/words") do |dict|
  dict.inject(Hash.new) do |all, word|
    all.update(word.delete("^A-Za-z").downcase => true)
  end.keys
end
What does this neat piece of code do?
    The last preparation step is to show Ruby code with highlighted syntax using Ruby itself. For this we will need to install a syntax gem.

Run on your host:

gem install syntax
Then create a simple Ruby script:
require 'rubygems'
require 'syntax/convertors/html'

convertor = Syntax::Convertors::HTML.for_syntax "ruby"

puts convertor.convert( File.read(ARGV[0]) )
    PHP provides a lot of different functions to run external commands, but looks like only 'exec' allows to capture output.

We will need it captured first of all to clean HTML special symbols out of it (second reason is noted at the end of this post).

Unfortunately 'exec' does not accept environment settings, so we will need to embed them in the command itself. We also want to be able to use ruby 'require' without tricks with getting current file location - it means that we should run ruby from the script location.

     
cover of Best of Ruby Quiz (Pragmatic Programmers)

Best of Ruby Quiz (Pragmatic Programmers)

Author: James Gray
List price: $29.95 USD
Amazon price: $22.76 USD


   

When I decided to open a Ruby section, I thought that it would be a good idea to have all examples powered by Ruby itself. In the middle of creation of PHP wrappers I've found that a) ruby version provided by 1and1 is 1.8.1 (pretty old), and b) gems are not installed.

Googling for possible solutions brought useful blog entries by Ed Johnson, Pascal and James Stewart (as I am not interested in Rails for now, I do not have Ed's problems).

After several attempts and playing with different configuration settings I finally succeeded - and all future Ruby posts will be powered by Ruby!

And, as one of my favorite programming principles - The Pragmatic Programmers' DRY (Don't Repeat Yourself) - naturally evolves to DAO TRAY (Don't Ask Others To Repeat After You), I've captured the sequence of commands in a shell script.

Syndicate content