The Ruby Greener Test Challenge is here. It is attempt to make Ruby on Windows better and more stable and direct goal of this challenge is to fix all tests and to reach 0F0E (0 failures, 0 errors) state. If you are Ruby developer on Windows you should consider taking a role in the challenge.
If you decide to try to fix some failing tests or existing errors it is easy to set up complete environment with the help of Ruby Challenge Pack. I will not write about details here since you have all you need in the initial challenge post given in the link on top of the article. Instead I will give you few suggestions about participating in the challenge.
You should be aware of three things when you run tests:
Set up console code page to 1252 or 65001. Some tests may fail due to the console code page, so before running tests you should execute chcp 1252 or chcp 65001.
Option -j2 may lead to errors. In case you want to run tests faster you can use make test-al TESTS="-qv -j2" but be aware that this can also lead to fals failures or errors.
Anti-virus software. If you see Errno::EACCES: Permission denied error in test, most likely the cause is anti-virus which locked the file during tests.
With these things on your mind you can proceed to fixing. Here is simple workflow you can use. The very first thing you should do is to clone Ruby source from GitHub:
After deciding what you want to fix you should make new local branch in which you work:
After you find a source of failures or errors you will make fixes and commit changes to your local branch. I would suggest making small commits. If commits contain too many changes, patches are harder to apply and are more likely to cause conflicts. Finally after finishing all fixes you just have to make a patch (es).
Adjust last number in above command to the number of commits you made on your local branch. This will create one or more patch files (one file per commit). In the case of already submitted fix single file was created: 0001-FileSystemObject-does-not-support-UTF-8.patch. At the end open a ticket, write small description, attach your patch file (s) and assign ticket to Luis Lavena.
Recently Luis Lavena started thread Idea: executable stubs to replace batch files on RubyInstaller mailing list. In short, the idea is to use command line applications - executable stubs - instead of batch files in RubyInstaller Ruby versions. Using command line applications instead of batch files has several benefits. First one, maybe not so important, is to avoid annoying
Terminate batch job (Y/N)?
question when execution is interrupted with Ctrl-C key combination. The second is to get meaningful list of processes in the system. With batch files we can only see bunch of ruby.exe processes in the list. This gives us possibility to define firewall rules for these applications which will not be applied globally for all Ruby scripts. Finally, installing Ruby applications as services, with the help of some service wrapper, is usually easier if we use executable file. These are reasons why new gem-exefy gem was made.
Gem-exefy mimics behavior of batch files installed by RubyGems and to see how it works we must know how existing batch files work. First step is to install gem that has executable value defined in gem specification. Example of such gem is Bundler. After installing it on Windows, RubyGems will create bundle.bat file in <path_to_ruby_installation>/bin folder. Content of that file is:
Without digging too much into all details we can see that batch file starts ruby.exe passing it full path to Ruby script (bundle, in this case) using all arguments passed to batch file as arguments of Ruby script. So all we have to do is to make application which will be able to execute Ruby script and will accept arguments passed in the command line. Sounds familiar, isn’t it? Exactly! We already have ruby.exe and in Ruby code we can find almost everything we need. Here is a slightly simplified version of Ruby’s main.c file:
If we build application from this source we will get the same application as ruby.exe. This means if we want to execute some Ruby script we will have to pass path to it as a first argument with optional arguments following it. But our goal is not to invoke application with the path to Ruby script. Instead we want to invoke predefined script. In order to achieve that, we obviously have to alter the list of arguments (argv) and to insert path to target Ruby script. But there is a catch (I spent almost whole day to figure it out). We must change the list of arguments after the call
Ruby performs system dependent arguments initialization in the above method and the list of arguments will be reverted back if we change it before this initialization. Of course there are some additional details that we must take care of, but you can figure them out directly from “the source”:https://github.com/bosko/gem-exefy/blob/master/templates/gem_exe.c. After we change the list of arguments, Ruby will execute script we passed it and that’s all the magic gem-exefy does.
We are now ready to exefy existing and new gems on our RubyInstaller version. gem-exefy is made as RubyGems plugin. After installing it with:
new gem command will be available - exefy. This command is used for replacing batch files for single or all installed gems. Replacing batch files with executable stubs for single gem is performed by passing name of the targeted gem to the exefy command.
Exefying all installed gems is simple - just pass --all to exefy command
If you are not satisfied and still want to use batch files - don’t worry. You can always revert old batch files for single or all gems with --revert argument
After gem-exefy is installed it will, by default, install executable stubs instead of batch files for all gems installed after it.
It is important to mention that gem-exefy will not replace batch files for commands installed with RubyInstaller (irb, rake…) and are part of Ruby core. Will support for converting these batch files be implemented is yet to be seen.
I use Windows on my everyday work. First thing I do after installing Windows is to set up complete environment for Ruby. In “ancient” days (measuring in computer years, of course) I have used One Click Installer. Later, RubyInstaller came on the scene.
With much better foundations, RubyInstaller simplified Ruby usage and made it look like on Linux, Ruby’s native platform. Key benefit of RubyInstaller over One Click installer was possibility to install Ruby native gems written in C. Switching from commercial VisualStudio, used in One Click Installer, to MinGW build tool-chain in RubyInstaller was crucial decision made by Luis Lavena, great author of RubyInstaller.
After RubyInstaller, one more project appeared with the goal to simplify Ruby on Rails installation and usage on Windows. RailsInstaller. I haven’t tried it, but I’m sure it will help increasing number of Rails users on Windows.
But do you really need installer for Ruby on Rails on Windows? It depends of your attitude. If you are not interested what is going on behind the scenes and just want to be able to start using Rails after few mouse clicks RailsInstaller is definitely for you.
If, on the other hand, you want to know how things work, you should try to install Ruby on Rails without installer. With RubyInstaller installing Ruby on Rails on Windows is, actually, just a matter of issuing
Of course you might face problems building and installing some gems, primarily because gem authors don’t want to bother with Windows support or simply because gems rely of third party libraries that are not ported to Windows. In any case you can ask for help on RubyInstaller mailing list.
People wonder if Ruby and Ruby on Rails are really usable on Windows. My opinion is that they are. Of course, there are some problems mainly with speed, but I would dare to say that solutions are on the way and hopefully very soon this will not be problem any more. I guess main reason why they are not so widespread on Windows platforms is scepticism and lack of experience.
As one of contributors to RubyInstaller project I noticed same questions are repeated on RubyInstaller mailing list. Mainly because of lack of familiarity with Ruby and RoR on Windows. That’s why I decided to write a book where I tried to give deeper insight on all details about installing and using Ruby and Ruby on Rails on Windows. The Ruby on Windows Guides is still in beta. However it already covers the most important issues and I think all novices and advanced developers can learn something new from it. Work on the book is still in progress so if you have any suggestions, ideas and wishes you can send them as an issue on the GitHub.
If you want to get deeper understanding of Ruby and Ruby on Rails on Windows, find out how to build native gems in several ways or just want to get an idea for what you can use Ruby on Windows go ahead and read the book. Of course I expect your comments, here or on GitHub.
Common problem that Ruby developers face on Windows is missing dll message box that appears when they try to use gem that has extension library. Usually these Gems are packed with pre-built binary\ extensions for windows and, even though, installation passes without any error or warning, when they try to use them they realize that dll these Gems depend on are missing on the system.
Maybe most common Gem for which this happens is sqlite3, Ruby binding for the SQlite3 embedded database. If sqlite3.dll is missing from the system, after installation of sqlite3 Gem any attempt to use it causes following message box to appear.
If two versions of a required dll exist on the system Gem might use the wrong one and strange crashes can appear.
These errors are not limited to the Gems that have pre-built binary extensions but can also happen if extension library was built with the RubyInstaller’sDevKit. If development files (header and library files) needed for the extension library to be built exist on the system while target dll is missing gem usage\ will cause the same message box to appear.
Frequently novice Ruby users on Windows ask question on mailing lists why Gem is not working even if it was installed without any error. That’s why I made dep_walker, small utility Gem, that can be used to check whether all dependencies for extension libraries used by installed Gem (s) are met or not. Source of the Gem can be found on GitHub.
Usage is very simple. If you want to check all installed Gems just invoke dep_walker with the ’-a’ switch.
And for particular gem swith -c can be used.
More verbose output is obtained via -t and colour with --color swith. Happy dependencies walking!
Testing is (or should be) important part of every software development. Over time various testing strategies and supporting tools and frameworks have been developed. Regarding Web development biggest advance has been made in Behavior Driven Development. Consequently many tools for BDD are published and used.
Ruby on Rails framework had great built-in support for testing from the very beginning. As it usually happens, lot of specialised testing tools appeared aside of it and among all of them my favourites are RSpec and Cucumber. The first one for unit and the second for functional tests. Both of them are well integrated with Ruby on Rails and are very easy to set up and start with. Moreover there is no need to use real browsers which results in fast tests execution. Perfect way for BDD.
Using Selenium in Cucumber tests is done through Webrat or Capybara. First we must set up complete environment, and in the first step all necessary gems must be installed:
Option —pre is used to install Capybara 0.4.0 rc
Important notice for MS Windows users: Webrat depends on Json gem which installs binaries compiled against Rubyinstaller Ruby 1.8.x version. If you are using 1.9.2 Ruby you must uninstall Json gem and install it again but with --platform=ruby option:
Since article focuses on the functional testing outside of Rails we should manually create folder structure that Cucumber expects.
All .feature files go in the features folder. In the support folder env.rb file should be created and within it all set up must be made. Finally steps are implemented in Ruby files in step_definitions folder.
Webrat controls Selenium through Selenium RC (remote control) and selenium-client gem. In order to use Selenium through Webrat put following code in your env.rb:
That’s all if you are running Linux based system. On Windows a little bit more effort must be made. First of all, Webrat usess 0.0.0.0 IP address when it starts Selenium and MS Windows does not like it at all. Secondly it uses /dev/null stream which is not available on MS Windows. Patch is already submitted and you can follow a ticket at Webrat Lighthouse. But until fix is accepted and new version is released, you can take a patch from Github gitst and apply it to Webrat sources.
Besides this patch few more things must be done. Line:
must be added to the config block as is already shown in the above snippet. Unfortunately selenium-client gem does not recognize Rubyinstaller since it is built using MinGW tools. Therefore one more tiny patch must be made in the selenium-client-1.2.18/lib/nautilus/shell.rb file. Function windows? must be replaced with:
You are ready for application testing. By default Selenium will use Firefox and if you want to use other browser (in the example Internet Explorer is set) add following line to config block:
Although Capybara can use Selenium RC, it primarily uses Selenium WebDriver which is still in beta phase but is working good. Since we already installed all necessary gems we can go on with configuring our testing environment. File env.rb should look like this:
Setting up Capybara is definitely much easier. But on MS Windows systems, if you want to use Internet Explorer, you still have to patch sources. Authors are already notified about required patch and I believe that new version of selenium-webdriver gem will be released with it. In the meantime you just have to change definition of initialize method in selenium-webdriver-0.0.28/lib/selenium/webdriver/ie/bridge.rb from:
Changing browser is as easy as changing :firefox to :ie or :chrome. Instead of :firefox you can also use :ff and for Internet Explorer :internet_explorer. One more notice about differences if you are switching from Webrat to Capybara. Capybara will reset session after each step. If you do not want that (for example you log in to your application in the first scenario, and do not want to repeat it in each succeeding one) just add:
in env.rb file.
With Capybara you are not limited to Selenium WebDriver. If you want to use Selenium RC you just need to configure it in env.rb file:
Although I’m still not sure which one is better to use, I switched from Webrat to Capybara and I think that tests that use Selenium WebDriver are running faster. There is an initiative for merging these projects into one but I do not know if it will happen and when. I would like to hear what you think. What is your choice: Webrat or Capybara?
After finishing first version of Rmre and issuing fix gem dependency in version 0.0.2, I got an idea for additional functionality. Why not use Rmre for dumping complete schema with all foreign keys data? What would be possible scenario for using this, one might ask? We have possibility to create ActiveRecord models in order to move to Ruby on Rails where main premise is to keep logic out of database and maintain it in application. Therefore we do not need foreign keys since we already have constraints defined in models.
But what if you cannot move to Ruby on Rails and you only have to change DBE, i.e. instead of MS SQL you must use Oracle? In that case you still have to work with legacy database from PHP or Hibernate in Java and “only” thing you have to do is to make create script for all tables but for another DBE. When database has hundreds of tables with lots of relations this can turn into nightmare, especially if you have to maintain both versions.
Rmre should simplify this. First you use Rmre to dump schema to some file and later you can use ActiveRecord’s capabilities to load it on different DBE. Since loading schema in ActiveRecord is DBE agnostic it should correctly create tables, indices and foreign keys on any database engine. That’s theory and, as usual, practice is a little bit different. On a very first step I’ve faced problem in MySQL database.
Let’s examine database with just a two tables - city and country. Create script would look like (example from Sakila database):
As can be seen from above script table city has foreign key on table country. Now let’s see what is result of a dump:
At the first glance this looks good but unfortunately doesn’t work. Problem is that loading this schema through ActiveRecord will create columns city_id in table city and country_id in table country as integer type but column country_id in table city is created as smallint. Defining constraint on columns which are not of same type is not allowed so last statement for altering table fails. At the moment I have no idea how to fix this and any suggestion is very welcome. I still have to check what happens on other DBEs: PostgreSQL, Oracle and MS SQL.
Very often I have to work on databases which do not follow ActiveRecord convention and making ActiveRecord models, if number of tables is large, is very slow and boring task. In order to speed up and simplify it I’ve created Rmre gem. Gem is quite simple yet you might find it useful if you want to create fixtures, migrations or simply port application to Ruby on Rails.
So how it works? For each table in the database, gem creates model. Name of the model is created using Rails classify method. Moreover, if table’s primary key is not column named “id” gem sets primary key by adding set_primary_key "primaryKeyColumnName" line to the model. In addition for MySQL, PostgreSQL, Oracle or MS SQL foreign keys are analyzed and for each constraint gem generates belongs_to or has_many lines. Here is model created for table store in Sakila MySQL test database:
I guess all Ruby and Windows users will be happy to hear that RubyInstaller team is about to release Ruby 1.9.2p0 very soon. See details in this thread. It will take some time to update documentation so be patient and keep an eye on the project home page.
After several successful updates of Ubuntu (starting from 8.10) last one was, more or less, complete mess. I was waiting impatiently for new Lucid Lynx to be released and started update as soon as I downloaded and burned DVD iso image. It seems everything goes smoothly till first reboot. Emacs was completely removed and new version was not installed. On my Compaq 8710w laptop with Quadro FX 1600M graphics card splash screen was displayed in low resolution and looked quite ugly. Moreover I was not able to install Emacs again.
I’ve decided to reinstall everything, so I’ve made backup of my home folder and start installation on a newly formatted partitions. Situation was a little bit better. I was able to install Emacs and all applications I need but problem with graphics card was still there as well as lousy splash screen. I’ve found few suggestions how to fix that by altering grub files and it really solved resolution problem but, at the same time, whole splash screen was shifted left and basically I was again on the beginning.
There is one more thing that annoyed me. I like Shere Khan Black Hand mouse pointers so I’ve installed that theme, but arrow pointer was still white, although some other pointers were changed (hand is small and black). Whatever I’ve tried arrow was keep staying white.
Then, after a few days, I saw black arrow and was really surprised. It turned out that it was unpleasant surprise because compiz stopped working and Nvidia driver was not enabled. Obviously it was time to try latest Nvidia drivers so I’ve downloaded and installed them. I must say that situation is now a little bit better - compiz is working, but I still have no luck with mouse pointers, nor with splash screen.
If Canonical’s goal is to increase number of Ubuntu users, they should really try to avoid such problems, since average user will certainly not be able to fix them and will switch to some other operating system.
I like to listen various Internet radio stations while I work. My favourite application was
streamtuner mostly because it had support for SHOUTcast. Unluckily new SHOUTcast design cannot be handled by streamtuner and future development on this application is
stopped. In other words there is a little chance for streamtuner to be upgraded so I’ve decided to make my own streaming directories browser - RStreamTuner (Ruby Stream Tuner).
As usual developing in Ruby was real pleasure and only doubt was what to use for GUI part. I’ve started with Ruby/Tk and left it very quickly due to very poor documentation (and probably lack of Tcl knowledge). I had
to find replacement and my primary goal was to find library that works equally well on Linux and Windows. WxWidgets with WxRuby gem was my final choice. Although RStreamTuner is still in early development
phase it is quite usable. At the moment it supports SHOUTcast and Xiph directories, but I plan to add more as soon as I find good one. Good candidate could be Live365 but they’ve made their pages quite hard to
parse so I’ll probably leave it for later.
Once I reach beta version, making support for new streaming directories will be quite easy and will require implementation of just a few methods. As a matter of act it is already so simple but I’m not
satisfied with the code and will change it once I find a little bit more time. Code is quite rough (I had little time to make RStreamTuner and was eager to get working version as soon as possible). Besides I
want to write detailed instructions how to add support for new directories. Anyway if you like to listen Internet radion stations and like Ruby clone RStreamTuner. Of
course if you have suggestions do not hasitate to drop me a note and if you make support for new streaming directory just send me a pull request and I’ll try to grab it as soon as possible.
At the and of May 2009 Savannah disks crashed. You can read more about it here. This is something no one can predict. You never know when your hardware will let you down, and there is no cure for that. Disks will be replaced, system reinstalled, backups restored and that’s it. Or is it?
Not completely if you are using CVS or SVN and there were some commits after the last backup. Emacs developers spent few days in the discussion to determine what commits they lost.
Luckily they had Git mirror of CVS repository but what surprises me is they didn’t move to Git yet. If they were using Git they would have been able to restore complete repository in a few minutes (maybe longer for
large repositories). Since they already have Git mirror for Emacs code I really do not understand why do they not switch to Git completely. Having full history at each developer’s computer is a huge benefit. Not
to mention that Git gives you possibilities you can only dream of in CVS and SVN.
Recently I had to process arrays of data with more than thousand of members but requirement was not to process them one by one. Instead I had to split array into chunks and to process each of sub-arrays separately. I’ve found one definition of chunk method within comments on this post and I’m including it here. This method splits array in given number of sub-arrays and I needed a version that will split array in the sub-arrays that have predefined size (ok, I know I could simply calculate a number of chunks by dividing total number of elements by required number of elements, but I just wanted one method that’ll do all that for me). So I wrote chunk_max_num method. If maximum number of elements per array is zero or negative it will return original array and for all other cases it will return array which has arrays of given size as members.
In my previous articles I wrote a lot about Emacs, its customization and adjusting it for Ruby and Ruby on Rails development. Although these articles give detailed
instructions how to set everything up, following all steps are somewhat cumbersome and require quite a lot of work.
Fortunately there is a solution. Just clone Emacs starter kit from Github and you will be able to develop Ruby and RoR applications in
Emacs in just a few simple steps. Emacs starter kit uses Elpa packaging and is very easy to configure. But let’s start from the beginning.
Clone Emacs starter kit with
Git, to your empty .emacs.d directory. Be sure that init.el file is in .emacs.d folder. If you do not use Git (do you realize what you are missing?) you can get archived sources if you
press download button on Emacs starter kit page on Github. Unpack archive to the .emacs.d and be sure that init.el is in .emacs.d folder.
Emacs starter kit has some packages that are still not in Elpa but you’ll have to install additional packages required for Ruby and RoR. Let’s do it now:
New buffer with list of available packages will open and you can select those you want to install. If you plan to use Emacs for Ruby and RoR you’ll definitely need Rinari. It will install all dependent packages.
cursor on the package line and press ‘i’. When you are finished with selection press ‘x’ and packages will be installed. At any time you can press ‘h’ to get quick help for Elpa packaging system. In order to
uninstall package you have to press ‘d’ (and ‘x’ after that).
When you are done with packages installation just restart Emacs and you are ready to continue work in your favorite language - Ruby and framework - Ruby on Rails :) But what if you want to adjust some
settings or keep some of those you set according to my previous articles? Luckily that is easy to do, too.
First create sub-directory in .emacs.d directory with your user name on the system you are using. Put your .el files there and Emacs starter kit will load them automatically during Emacs start up. If you have changed
color theme and you’ve used theme from color-theme library put color-theme-library.el in this folder and add, for example, line
to any of .el files in this folder. I keep all my additional settings in customization.el file.
I already mentioned yasnippet and yasnippets-rails packages. Simplest way to install yasnippet library is from Elpa system following above mentioned procedure. Unfortunately yasnippets-rails cannot be installed that way so you should clone it from the Github:
Directory yasnippets-rails should be in the directory with your user name under .emacs.d. Add following lines to your customization.el file:
and you will be ready for using new snippets during development.
My previous articles were based on Rails reloaded package for RoR development in Emacs. It is good library and if you like fancy GUI things you can still use it. There is a new version on Github.
Still, I would recommend trying Rinari that is a package from Elpa. It doesn’t have any GUI features but once your fingers “learn” all shortcuts your RoR development will be much easier and faster.