Distributing your Ruby program as a standalone executable (exe)

Since ruby is an interpreted language, whenever you want to release your program you need to release the source code. Also your program users must install Ruby virtual machine before they can run your program. Is there a way to distribute ruby program as a standalone executable?

Erik Veenstra has created a nifty tool called RubyScript2Exe which can convert your ruby program to an executable program. It achieves this by combining ruby interpreter code and your source code files into a single exe file.

First download the RubyScript2Exe tool from here. It is a ruby program (950kb size). Create a simple ruby program called helloworld.rb and save it to the same location where RubyScript2Exe.rb is stored.

From the command prompt, type the following,

ruby rubyscript2exe helloworld.rb

You will see the following output on console. Now you have a standalone executable program (helloworld.exe) which can be executed in any Windows machine! Combined with Win32API, you can create cool command line apps for Windows.

creating windows exe using ruby

The only problem with this executable is that it contains the entire ruby VM and hence is large (helloworld.exe size is around 1.5MB). Also note that this is not really a ruby compiler. It doesn’t convert your ruby code to machine code. It only packages ruby virtual machine along with your ruby code. Hence the code is still interpreted.

RubyScript2Exe can also create executable files for Linux and Mac OS X (Darwin). RubyScrip2Exe is released under GNU GPL license.

Accessing Windows API from Ruby - Using Win32API library

Ruby is used extensively in Web applications thanks to Ruby on Rails. But you can also develop Windows applications using Ruby. Interestingly the Windows API support is part of the standard installation (only on Microsoft Windows installations) and it is quite easy to use it!

In order to use Windows API, you need to import “Win32API” library in your applications.
You can then use the Win32API class to create a reference to an API method and can call it. The following sample program shows how this can be done. This application will display a native Windows message box,

When you run the above program, you will get a message box similar to the following one,

Now let us analyze our sample program in depth. The first line imports the standard Win32API library. Then we create two variables (message and title) which will hold the message to be displayed and the title of the message box.

Now using Win32API.new, we are creating a new instance of a Windows API method. The first parameter (user32) indicates the DLL which contains the API method. In this case the DLL is user32.dll. The second parameter is the name of the API method (MessageBox). The third parameter is an array indicating the type of all the parameters passed to API method (MessageBox). In the array, L stands for long number and P stands for pointer to string (string variable). The last parameter indicates the return type. In this case I stands for Integer.

Now using call method on the API reference we invoke MessageBox method. Note that the type of parameters matches the third array parameter in the constructor. First parameter is the id of the parent windows (in this case there is no parent and hence 0) and the last parameter indicates the type of message box (0 indicates default message box).

Following is another sample ruby program which uses Windows API to print out the logged in user name and also the computer name,

In the above sample, note that the second parameter is a pointer to the size of the first parameter. In Ruby there are no pointers and hence the size needs to be passed as a string value. Also we need to use unpack method to remove the trailing null character returned by API call.

Check out the following API definition for GetUserName (from MSDN),

lpBuffer
A pointer to the buffer to receive the user’s logon name. If this buffer is not large enough to contain the entire user name, the function fails. A buffer size of (UNLEN + 1) characters will hold the maximum length user name including the terminating null character. UNLEN is defined in Lmcons.h.

lpnSize
On input, this variable specifies the size of the lpBuffer buffer, in TCHARs. On output, the variable receives the number of TCHARs copied to the buffer, including the terminating null character.

As you can guess, the parameter passing and parsing return values from API quickly becomes tedious. So from a productivity angle using Windows API to build full fledged Windows applications is not recommended.

Ruby-Talk - rb_str_new2 cleanup, ruby GUI libraries and Ruby vs Python

Note: This is a weekly round up of interesting stuff found on ruby-talk mailing list. You can subscribe to this list here.

1. Steven wondered whether it is necessary to clean up the string created from native C code(rb_str_new2) and passed to rb_funcall. Matz replied,

You don’t have to (and you shouldn’t) free string objects.

2. Mohsin asked which is the better Ruby GUI Library, Ruby-GTK or FXRuby?. Both seemed good candidates and there were some additional recommendations such as JRuby + Swing + Monkeybars(James). Daniel recommended WxRuby,

I’ve had very good results with WxRuby http://wxruby.rubyforge.org/; It didn’t take long to learn, is NOT very rubyish (basically the C++ APIs are reimplemented in Ruby), but was very useful for cross-platform development. So you might want to consider that as a third option.

3. There was a big discussion (started by Marc) on differences between Python and Ruby. Michael had some good points on Ruby,

a. Flexible syntax of Ruby

b. Enough support for functional programming without having to fight with the language

c. A friendly community - he laments that this is on the decline!

d. No syntactic whitespace as in Python

As Ruby and Rails is becoming a mainstream language/framework it is only natural that you get more of the RTFM type of questions in the mailing groups. In my opinion there is no point in bitching about it, but rather politely point to the relevant documentation.

As far as Rails is concerned it is a big mess. The Rails 2 is out, but there is no documentation for a beginner in Rails 2. All the books are still stuck at earlier versions and for any documentation available online you need to pay! The only book available (3rd edition of Agile Web development with Rails) is still in beta and requires a lot of rewrite before it can be useful. So it is only natural that you get basic rails questions even on Ruby mailing lists and forums!

In another thread Mohit gives this explanation,

I see this as a sign that Ruby is picking up and gaining traction in other places.  There was a time when there were a lot of people who were discovering Ruby on their own free will and were looking at documents (online or offline) and trying out different things.  As it gains traction, it approaches the point where people are having Ruby thrust on them (possibly more Rails than Ruby) either due to preference of people further up in the food chain or due to clients (specially in the case of outsourced projects).  In such cases, it’s likely that the engineer is just venting frustration about the language on a forum cos it’s something they werem’t born to, or achieved - it was thrust on them.

4. Tim wanted to know the best way to delete every other value in a ruby array.  For example [a b c d e f g h i j k] becomes [a c e g i k]. There were a number of solutions proposed,

Harry,

Yermej,

Joel,

10 unique ruby language features

Learning Ruby is easy if you already know a programming language. But there are some ruby language features which can cause headache for a programmer migrating from C++ or Java. In this post, I will look at 10 interesting language features that are unique to Ruby.

1. Objects everywhere! - In Ruby everything is an object including simple numeric values. Here is an example,

2. Blocks - Blocks is a powerful feature in Ruby which simplifies programming. Blocks are code blocks which can be passed as a parameter to a method. Using this feature it is easy to build code libraries which can delegate varying functionality to code blocks to be built later. Here is an example which prints out all elements in an array,

3. Implicit return value in methods - Value of the last expression in a method becomes the return value of the method. In Ruby return keyword is optional. In the following example, 8 is the return value,

4. In Ruby everything is open! - In Ruby you can easily extend classes and modules. This means that nothing in Ruby (including built in classes and modules) are closed!. Interestingly additional methods can be added to a class even at runtime. Here is an example in which we add a previous method to FixNum class (which is the data type for all integers),

5. Missing unary operators in Ruby - Unary operators ++ and — are not supported in Ruby. Instead you can use += operator.

6. Ruby supports parallel assignment - It is possible to change multiple variables in a single assignment. The best example is the swapping of two variables as given below,

7. In Ruby strings are mutable - In Ruby it is possible to change a string variable in place. Hence unlike Java, the same string literal when used multiple times will point to different object instances.

8. True and false in Ruby - In Ruby only nil and false evaluate to false. This means that everything else evaluates to true! Hence even the value 0 evaluate to true in ruby. Following code snippet will output “Hello World” on console,

9. Native support for ranges and regular expressions - Ruby language has native support for regular expressions and ranges.  This is backed up with a rich set of API methods. Control structures such as a case statement natively supports ranges for value comparison.

10. Method indicators - In Ruby the last character of a method name can indicate its behaviour. If the method ends with a question mark it indicates that the return value is boolean. If the method ends with an exclamation, it indicates that the method can change the state of the object. In many cases a non exclamation version of the method is provided which modifies a copy of the object.

What is a ruby symbol? - symbols explained

Many programmers who are new to Ruby get confused when they see Ruby symbols. A lot us came to know about Ruby language through Ruby on Rails projects. In Ruby on Rails, symbols are everywhere! So it is essential to understand the concept of symbols in Ruby.

A symbol in Ruby is an instance of the class Symbol. A symbol is defined by prefixing a colon with an identifier. :name, :id, :user etc. are examples of symbols.

Let us see what is there in the class Symbol.

Symbol class in Ruby contains one class method all_symbols and instance methods id2name, inspect, to_i, to_int, to_s and to_sym.

  • all_symbols - Returns an array of all the symbols in Ruby’s symbol table.
  • id2name - Returns the string representation of the symbol - :name.id2name returns “name”.
  • inspect - Returns the symbol literal
  • to_i - Returns an integer unique for each symbol
  • to_int - Same as to_i
  • to_s - Same as id2name
  • to_sym - Convert the symbol to a symbol!

Symbols are most commonly used in creating hashes. For example, consider the following hash,

We could have used strings instead of symbols in this case,

But the advantage in using symbols is the efficient use of memory. Maximum space taken by a symbol is never more than the space taken by an integer. This is because internally symbol is stored as an integer. In case of strings the memory space depends on the size of the string.

Also whenever a string is used in the program, a new instance is created. But for symbols, same identifier points to the same memory location! This can be easily checked by running the following,

The memory saved may look trivial in this case. But when a predefined hash key structure is used as parameter to a method and if the method is used many times in the program, the savings can be substantial! For example, in RoR a sample link is defined as,

The first parameter is the label used for the link and the second parameter is a hash. The second parameter is defined as a hash to enable dynamic parameters.

Now in the implementation of link_to method, a check is done to see if :action key exists in the hash and then corresponding value is used. Similarly :controller is also checked and if found is used. Hence using hashes is a good way to implement optional parameters.

In this example we could have used “controller” and “action” strings instead of symbols :controller and :action. But we will be missing two important advantages - efficient memory usage and speed of processing.

In a rails application, you might be using link_to hundreds of times. Assume that you have used it 10000 times in a big application. If we assume every call contained :controller and :action symbols, total memory required by the symbols = 2 * size of an integer. Whether we call link_to once or millions of times the memory taken remains same. But things would be different in case of strings as keys. Whenever a link_to is called 2 new strings “controller” and “action” will be created. Hence a total memory allocation of 10000*16 bytes would have done by Ruby interpreter (of course garbage collector will ensure memory usage at any point to be much less than this).

Second is the question of speed. We know that link_to internally compares hash key to :controller and :action. This is extremely fast since it is equivalent to comparing integers. But if we had used strings, comparison such as “controller”==”controller” is inefficient since it involves comparison of all the characters in it.

Strings are scoped and they get destroyed/garbage collected when the execution goes out of scope. But in case of symbols, they remain defined as long as the program is running. So if you have a very large number of symbols there could be a lot of unusable memory.

It is possible convert a string to symbol using the to_sym method. Hence “controller”.to_sym returns :controller. Symbol identifiers can be quoted and they can also contain special characters. Hence :”wow, this is a symbol!” is a valid symbol.

In short, symbols are used instead of strings wherever we care only about the identity and not the content. Why we use them? Because they are memory efficient and comparison operation on them are really fast.

Now if you are still confused about symbols, just forget about them. In time you will realize what they are! :-)

Using temporary files in Ruby - Tempfile.new

For certain programming solutions, you might need a temporary file. Different operating systems store temporary files in different locations. Also you don’t want to explicitly name the file since that is irrelevant in the program. How do you ensure that there are no filename conflicts? The solution for all these is to use library for temp file processing.

Ruby comes a with a default library ‘tempfile’ for handling temporary file creation. Given a filename prefix, it creates a temporary file in the following format.

[prefix]-[process].[unique_number]

This ensures that there is no conflict in creating multiple temporary files in the same directory. Once your program terminates the temporary file is automatically deleted. Also by default, the temporary file is created in the operating system’s temporary folder (In my ubuntu system it is /tmp).

In the following program, a temporary file with prefix ‘random’ to store 50 random numbers is created. When the program terminates you cannot see the temp file. Hence I have added a ‘gets’ which stops for user input. In my case I could see a temporary file named ‘random.5789.0′ under \tmp dir.

If you want to the temp file in a specific directory, you can pass the directory name as the second parameter to new. For example - Tempfile.new(’new1′,’/usr/home/jay’)

A Quick Dose of Ruby History

When you are learning something new, the first thing you should check out is its history. When you go through the history, you get a better perspective. It is the same with Ruby language and hence let us get a quick dose of Ruby history.

Ruby programming language is created by a Japanese programmer, Yukihiro Matsumoto. He is popularly known as “Matz” in the Ruby community. Matz started work on the language sometime in February 1993. The first alpha version was released in December 1994. Since then there has been numberous releases and the current production release 1.8.6 was done on March 2007. On 25th December 2007 there was a development release of Ruby 1.9 (this is not intended for production systems).

Ruby 2.0 will be the next major release and it is intended to expand Ruby lanugage and to improve the interpreter performance.

Matz main goal in developing Ruby was to create a scripting language that was more powerful than perl, and more object oriented than Python. Matz named the language after the “Ruby” gemstone and is inspired by “Perl”. Major guiding philosophy for the language has been “the principle of least surprise”. The first thing many new programmers note in Ruby is its elegance.

Following are the key turning points in Ruby history,

  • 1993, Feb 24 - Matz starts work on Ruby
  • 1993, April - First “hello world” works in Ruby
  • 1995, December 21 - Ruby 0.95 was released to the public
  • 1996, December 25 - Ruby 1.0 was released
  • 1998, December - ruby-talk mailing list was started
  • 2000 - Official Ruby newsgroup started
  • 2001 - RubyConf started
  • 2001, December 15 - Pickaxe book on Ruby was released
  • 2003, August - Ruby 1.8.0 was released
  • 2004, July - Web application framework Ruby on Rails released
  • 2007, March - Ruby 1.8.6 was released
  • 2007, December 25 - Ruby 1.9 (experimental version) was released
  • ??? - Ruby 2.0 release in future

Using Ruby’s http library - download and process web pages - I

Ruby has excellent networking support. Ruby has low level networking features such as sockets and tcp/ip protocols. It also has a high level API for handling protocols such as http and ftp. In this post we will look the Ruby http library. We also look at how this library can be used to download and process web pages.

1. Downloading a web page using Ruby

Following code illustrates using net/http library for downloading the Google’s home page. You should see the google homepage html in console!

Now in my machine, this returns text which says “document has moved”. This is because google is send a redirect to www.google.co.in. Following code shows how we can handle http redirect.

Now how do we rewrite this if we need to use a proxy server to connect to internet? In Ruby it is pretty simple. Check out the new version below.

Rails controversy - Zed Shaw gets Dreamhost support!

All hell broke loose after Zed Shaw (the guy behind Mongrel) wrote a post claiming Rails is Ghetto. In the post he points out the ugly part of “Rails Community”.

“This is that rant. It is part of my grand exit strategy from the Ruby and Rails community. I don’t want to be a ‘Ruby guy’ anymore, and will probably start getting into more Python, Factor, and Lua in the coming months. I’ve got about three or four more projects in the works that will use all of those and not much Ruby planned. This rant is full of stories about companies and people who’ve either pissed in my cheerios somehow or screwed over friends..”

Now Dreamhost (one of the biggest web hosts out there) comes with a rant on the technical problems with Ruby on Rails. Following sums up the whole post.

I don’t have anything to add to Zed Shaw’s comments about how the Rails development team operates as I don’t have any personal knowledge of that. What I do have personal knowledge of is how difficult it can be to get a Rails application up and running and to keep it running. DreamHost has over 10 years of experience running applications in most of the most popular web programming frameworks and Rails has and continues to be one of the most frustrating.

The main problems pointed out by DH blog are,

  • Performance problems
  • Poor support for shared hosting platform
  • Backward compatibility issues

Also worth noting…

It’s all good and fine to recommend that users use higher end dedicated server hosting for their commercial applications but you simply cannot ignore the fact that nearly everyone will want to use lower cost shared hosting for getting started. It’s just simple economics. Additionally, people who use systems like Ruby on Rails want to spend time programming and not time setting up servers. Recommending technologies that are not widely used or supported by any major web hosting company is putting too much of a burden on your users, the people you want to keep happy! It’s a good thing we never even tried to switch our system to support Lighttpd and SCGI, too, because 6 months later the ‘in thing’ in the Rails community had shifted to Mongrel, instead.

Advanced Ruby - Dynamic code execution

One of the advanced features of Ruby, which is behind the success of rails is the dynamic code execution. Code fragments can be executed using eval method. This is very similar to JavaScript eval function.

following is an example,

eval executes the code in the current context. It is also possible to execute the code in a different context.

Code can also be executed in class context using another method class_eval. class_eval can add methods due to a class during execution! In the following example, we add a method doHello to the Sample class during execution of initialize. Then we execute doHello!

Now this is the technique which is behind the secret of attr_accessor used in classes. In fact attr_accessor is just a method. We will try to create a similar functionality using attr_accessor2. All the classes in Ruby are subclassed automatically from the class Class. Hence we will modify Class and add attr_accessor2 method which in turn will add getter and setter methods for the passed variable!