rulururu

post Determine OS with ruby

June 1st, 2009

Filed under: Ruby — Kai @ 6:30 pm

I’m developing a tiny application that should run on (almost) any operating system. Though to that I sometimes have to do some switches (e.g. for console coloring).
The constant RUBY_PLATFORM helps me, so that I wrote a small module

module OsHelper
 
  def is_linux?
     RUBY_PLATFORM.downcase.include?("linux")
  end
 
  def is_windows?
     RUBY_PLATFORM.downcase.include?("mswin")
  end
 
  def is_mac?
    RUBY_PLATFORM.downcase.include?("darwin")
  end
 
end

Unfortunately I quickly realized that it’s more a bad than a good idea because RUBY_PLATFORM will return 'java' when using JRuby for example.

I found sys-uname library that gives much more information than that constant can do. (gem install sys-uname)

Finally you can use it like that:

begin # use Sys::Uname library if present
        require 'sys/uname'
        @@os_name = Sys::Uname.sysname
        @@architecture = Sys::Uname.machine
        @@os_version = Sys::Uname.release
      rescue # otherwise use shell
        @@os_name = `uname -s`.strip
        @@architecture = `uname -p`.strip
        @@os_version = `uname -r`.strip
      end

I hope it someday will help someone ;)

post no such file to load — mkmf

May 3rd, 2008

Filed under: Linux, Ruby — Kai @ 2:05 pm

When writing a ruby script I need an external library for Id3Tags. I tried to install it using gem (RubyGems is a packaging system -> Everything you need to know about).
It really took me some time until I could solved the problem:

Updating Gem source index for: http://gems.rubyforge.org
Building native extensions.  This could take a while...
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
        from extconf.rb:1

There is no package that has the name mkmf or something like that.
Thankfully, Google led me to the answer on RubyForge. For some reason, mkmf.rb is part of the ruby1.8-dev package, and initially I had not installed that since it is described as

Header files for compiling extension modules for the Ruby 1.8

A quick & easy

sudo apt-get install ruby1.8-dev

and everything trotted along happily after that. Don’t ask why I hadn’t already installed that development package.

EDIT: Maybe, it kinda seems like, it was not my fault not install ruby1.8-dev package, it seems to be a but in Ubuntu’s package depencies.

post Several ways to grep lines

April 24th, 2008

Filed under: Linux, Ruby — Kai @ 12:17 am

Especially when accessing logfiles or large configfiles you often look for a particular pattern in it.

Everybody working with Linux shell usually appreciates grep.
I will just give you a short overview before getting around with the really worth knowing things:

All lines that match contain “EE” in general.log or mylog.log

grep -i 'd' general.log mylog.log

To lists the names of all files in the current directory whose contents mention “EE”.

grep -l 'EE' *.log


grep -lv
lists the names of all files containing one or more lines that do not match. To list the names of all files that contain no matching lines, use the -L or --files-without-match option.

You also can use regular expressions if needed.

For exclusively displaying lines starting with the string “root” just type:

grep ^root /etc/passwd

A cool addon to grep is egrep which can be used like sed (which shouldn’t be an issue here) to find & manipulate at a single blow.

This should delete all comments in the apache config.

egrep '^[^#]' /etc/apache2/apache2.conf

At least this is hardly correct, to match the comments it needs a bit more because they are most times followed by some whitespaces.
Effectively remove those lines:

egrep -v '^ *(#|$)' /etc/apache2/apache2.conf

If you’re a fan of ruby you might like this.
In ruby you need only request that your string be tested against the regular expression:

ruby -pe 'next unless $_ =~ /regex/' < test.txt

For instance to get every line of test.txt that contains a time in the given format (HH:MM:SS):

ruby -pe 'next unless $_ =~ /(^|\s)[0-9]{2}:[0-9]{2}(:[0-9]{2})(\s)/' < test.txt

To print only lines of 30 characters or greater:

ruby -pe 'next unless $_.chomp.length >= 30' < test.txt

It’s just the beginning of easy matching with ruby, as you might expect,there are many more undreamed-of possibilities.

ruldrurd
Powered by WordPress, Content and Design by Kai Bellmann
Entries (RSS) and Comments (RSS)