Tagged: howto

Learning Ruby

I always wanted to learn Ruby. It became so popular over the last couple of years and I hear people praise the language everywhere I go. Well, time has come and I cannot postpone this anymore (not with a clear conscience anyway). So I’m finally learning Ruby. I went quickly over the net and also our campus library today, to see what resources are available for the Ruby newbies.

Resources

There is a load of good resources on the internet, so before you run into a bookstore to buy 3000 pages about Ruby, consider starting with the online books and tutorials. You can always buy something on paper later (I personally like stuff the old-fashioned way — on paper more). Here is a list of what I found:

That would be some of the online sources for Ruby beginners. And now something on paper:

  • The Ruby Way by Hal Fulton — Great, but sort-of-a big-ass book. Just don’t go for the Czech translation, it’s horrifying
  • Learning Ruby by Michael Fitzgerald — A little more lightweight, recommend ford bus-size reading!

I personally read The Ruby Way at home and the Learning Ruby when I’m out somewhere. Both of them are good. These are the books that I read (because I could get them in the library). There is a pile of other titles like:

Just pick your own and you can start learning :-).

Installation

Ruby is interpreted language so we will need to install the ruby interpret. On Linux it’s fairly simple, most distributions have ruby packed in their software repositories. On Fedora 15 write sudo yum install ruby. On Debian-based distros sudo apt-get install ruby. If you are Windows user, please, do yourself a favor and try Ubuntu!

To check whether the ruby interpret is available go to terminal and type

 $ ruby --version

Hello Matz!

The only thing that’s missing is the famous Hello World :-)! In Ruby the code looks something like this:

#!/usr/bin/env ruby

puts "Hello Matz!"

Summary

From what I saw during my yesterdays quick tour through Ruby, I can say, that it’s a very interesting language. I’d recommend anyone to give it a shot! I definitely will.

Update: Stay tuned for more! I’m working on a Ruby language cheat sheet right now.

Sources

Advertisements

Syslog Howto

Syslog is a standard for logging program messages. It allows separation of the software that generates messages from the system that stores them.

Format

The traditional use of a syslog daemon is to store events in log files, in a format like this:

Jan 1 12:12:12 10.245.3.99 foo[421]: this is a message from foo

All the log files are in the /var/log/ directory. You can explore the files on your own machine an see the log messages for yourself.

Writing your own messages

Now, how can you log something with syslog? There is a command-line utility suited just for the job — logger. For instance:

$ logger -t "dude" "What's up New York?"

will add something like

Jul  8 10:45:53 astro-desktop dude: What's up New York?

into /var/log/messages.

Logger is most useful for bash scripts, but while in C, you’d probably like to use something else. So let’s have look at some syscalls. Here is a very simple program, that will do the same as the logger command shown a couple lines back.

/* Example of using syslog. */

#include <syslog.h>

int main()
{
  openlog("dude", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);
  syslog(LOG_NOTICE, "What's up %s?", "New York\0");
  closelog();
}

Function openlog() establishes connection to the logging system, then you can start using syslog() to save some messages and in the end you’ll need to close the connection by closelog(). Fairly simple :-).

There are some parameters to the functions which are described in man syslog or somewhere on the net.

Sources: