Posts

Rails 3 "couldn't parse YAML" error suddenly showing up

I have been developing on the latest Rails 3, Ruby 1.9.2 for a while. and suddenly in 2 projects I started to get this YAML loading error when I try to start my server or run the tests. bundler./home/mahmoud/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/psych.rb:148:in `parse': couldn't parse YAML at line 55 column 12 (Psych::SyntaxError) from /home/mahmoud/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/psych.rb:148:in `parse_stream' from /home/mahmoud/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/psych.rb:119:in `parse' from /home/mahmoud/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/psych.rb:106:in `load' It turned out to be related to my recent upgrade of bundler to 1.0.10 After this update, my database.yml was not considered a valid YAML file. until I realized I have to put the special character values (for example: asd@#$asdf@$@) between double quotes. This fixed the problem for now I also came across another approach that basically will use a different YAML engine, This is wi...

Recovering GRUB after windows installation

I finally used my windows upgrade kit to upgrade my laptop from windows Vista to Windows 7. This has been too much delayed because I use ubuntu lucid mostly anyway :). One of the side effects during the upgrade is to load the GRUB loader menu from which I select the OS to load. Windows overrides that upon installation so it is completely by passed. Luckily, this is a well covered topic at ubuntu forums since it is a likely thing to occur A nice guide on how to restore the grub menu can be found at https://help.ubuntu.com/community/Grub2#Reinstalling%20from%20LiveCD This is useful if you are using GRUB2, ubuntu 9.10+ To summarize: Use a live CD to boot ubuntu on you computer mount the system partition (or the partition having the /boot directory) in the original installation of ubuntu that you have to a specific location (say /meda/ubuntu_sys) use the grub-install command: sudo grub-install --root-directory=/media/ubuntu_sys /dev/sda Reboot your system and , voila, ...

Silence

It has been a long while since I updated my blog.. I guess tweeting became an quick way to express and share my thoughts.. although my tweets are mostly non technical :) If interested, follow me at @modsaid

chrome/blogger incomptability ?!!

Image
This issue has been buzzing me for a while now since I started using the Linux version Google Chrome . For some reason, when I try to publish a comment on a blog post on google's blogger.com, I get nothing, absolutely nothing, the page just refreshes when I click the publish button. I have to move and open the page in Firefox in order to proceed posting my comment. I hope whatever the conflict was, that chrome team fix it on this beta version.

Ruby script for downloading rails casts

Ryan Bates continuously provide useful Rails tips in this railscasts.com . I had written a bash script for downloading the videos of the episodes in the past. I do not like streaming much and I'd rather keep the episodes for reference. One downside of the script is that I had to check the rss feed or the website for new episodes, and run the script manually passing the correct paramters (start and end of episode ids to be downloaded) Now I decided to get rid of the silly step. so I ported my script to ruby, and it runs down to download whatever episodes are missing using the rss feed and checking what is already present. The script uses simple-rss gem, so you'll have to get that installed. It also uses wget for downloading. I didn't bother using net/http of ruby, wget is fine for me as a linux user. The script flow is simple: Check the current files in the download directory (collecting information) Grab the rss of railscasts.com Iterate over rs...

Gmail search and labels.. a wished feature

Gmail has been my main email engine for years now. I'm totally comfortable with it. The ability to process, distribute and keep up with the flood of emails i get everyday from different resources, and the ability to search and find emails (even the very old ones) easily are the keys behind my loyalty to gmail. Gmail supports some nice techniques that can be used while searching emails. Besides what you can use in the advanced search form. the command line approach (direct control from the simple search box) proves to be even more powerful. Anything that can be controlled in the advanced search form, can also be done from the simple one provided that you know the right parameters. You can specify: to, from, subject, a label, ... What's more exciting is that you can specify several labels to search in, and several labels to be excluded from search. By default you have "read", and "unread" labels. A frequent search query of mine is "l:inbox l:unread...

Getting the sound to work on Ubuntu Jaunty

After installing Ubuntu 9.04 (jaunty) on my new HP DV6-1280us machine, my main problem was that the sound is complete dead. I see no errors when I try to play anything. there is just no sound. taking a closer look at my Sound Card: ~$ lspci | grep -i audio 00:14.2 Audio device: ATI Technologies Inc SBx00 Azalia (Intel HDA) 01:00.1 Audio device: ATI Technologies Inc R700 Audio Device [Radeon HD 4000 Series] After a while of googling around, I came across this post on ubuntu forums If you have the same sound card. (which is in most DV6, DV7 series of HP) This will probably be useful to you as well. Briefly, you'll need to download, compile and install the alsa driver. and modify configuration of alsa-base.conf as follows: wget -c ftp://ftp.alsa-project.org/pub/driver/alsa-driver-1.0.21.tar.bz2 wget -c ftp://ftp.alsa-project.org/pub/utils/alsa-utils-1.0.21.tar.bz2 wget -c ftp://ftp.alsa-project.org/pub/lib/alsa-lib-1.0.21.tar.bz2 tar xvf alsa-driver-1.0.21.tar.bz2 tar xvf alsa-lib-1.0...

Presenting NeverBlcok in rubykaigi 2009

Mohammad Ali's presentation of NeverBlock .

Patron: a Ruby HTTP client library

I was just trying out Patron , the new ruby client library, by Phillip Toland . Patron is based on libcurl. and providing nice usable interface, which allows you to make http requests easily and with as much customization as you need. Let us take a quick usage sample here: irb(main):036:0> require 'rubygems' => false irb(main):037:0> require 'patron' => false irb(main):038:0> sess = Patron::Session.new => #<Patron::Session:0xb7a9f878> irb(main):039:0> sess.base_url = "http://www.google.com" => "http://www.google.com" irb(main):040:0> resp = sess.get("/intl/en/about.html") => #<Patron::Response @status_line='HTTP/1.0 200 OK'> irb(main):041:0> puts resp.body Checkout the documentation for more samples and usage notes.

problems connecting to yahoo server with pidgin under ubuntu

Few days ago I noticed that pidgin is unable to connect to my yahoo account. I decided to have a look at the matter and realized that Yahoo has changed the way of the authentication as part of upgrading their software. pidgin released a new version that uses the new method (2.5.7). so I had to upgrade my 2.5.2 to the latest version. There was something wrong when i tried to follow the instructions on pidgin website. So I went to downloading the source code , following the instuctions in the INSTALL file to compile, build and install pidgin. I needed also to upgrade libpurple to 2.5.7 by: apt-get remove --purge libpurple0 apt-get install libpurple0 Finally, I was able to connect to my yahoo account :)

goosh.org The unofficial google shell

As a linux user, I spend most of my time using the shell to do most of the work. I am a command line fan like a lot of people out there. goosh.org is a command line based (Ajax web based) interface for google, created by Stefan Grothkopp . You just write your search query and the results will be listed for you in a very similar manner to command line programs. Searching using goosh will still need you to use your mouse to open the results and continue browsing. Not a very suitable shell mode. Well, this haven't been the main usage of goosh (for me). Instead, I have been using it heavily as my translator. Instead of: Going to http://translate.google.com/ in the browser Writing the text to be translated Choosing the from/to languages from the drop down lists Clicking "Translate" Goosh lets you get this by: Going to goosh.org typing the command following command and hitting Enter translate src dst sentence for example: translate en ar my name is mahmoud to get "اسمي...

Adding Custom Routes for Radiant Projects

While integrating simple_captcha into the guestbook extension of radiant, I had to add a special route for the simple_captcha to work properly. Unfortunately, the routes file of a radiant project does nothing but loading the routes file from radiant gem itself: load File.join(RADIANT_ROOT, "config", "routes.rb") Calling ActionController::Routing::Routes.draw again will clear all the routes that have been defined in the gem routes file. So you have to either modify the code of the gem, or copy the code from routes.rb of the gem radiant to the projects route file. Or you can append your new routes using add_named_route instead of draw . And since the simple captcha route is supposed to be defined before radiant routes. you can manipulate the routes array as in the last line of the following sample. load File.join(RADIANT_ROOT, "config", "routes.rb") ActionController::Routing::Routes.add_named_route(        'simple_captcha',        '/...

A closer look at ruby gems

Have you ever been stuck at "Bulk updating Gem source index for http://gems.rubyforge.org/" while installing a gem?!Due the to the latest drop in the Internet connectivity in the middle east and my own country, Egypt, installing gems started to be like hell.. I had to wait for hours before having my library installed so I decided to have a closer look. surprisingly.. it was due to a bug in rubygems 1.0.1. You can easily overcome this by updating to the latest gem 1.3.1 through gem update --system Gem downloads libraries metadata about all available gems in a gem server. by default we have gem source "http://gems.rubyforge.org/" and recently i added "http://gems.github.com".gem saves this data in a cache file before it performs the required operation, so it doesn't have to download it again the next time. Part of the code that reads the cache looks like this: MARSHAL_FIELDS = { -1 => 16, 1 => 16, 2 => 16 } #... # Load custom marshal format, re...

Prawnto, Generating PDF in Rails Applications

Rails wiki lists a number of libraries that can be used to generate PDF files in ruby . Prawnto was very suitable for my need (generating PDF files that contain text with differentfont-size and adding some formatting). Prawn is able to generate more complex PDF documents including adding photos. You can check the installation steps online Prawnto requires you to add code to your controller and to a .prawn layout file. There are several demos available online and can be used as guidance. Below is an example for generating a PDF format of an article. The article has a title and an array of blocks , each block has title and content . The controller code is: def show    @title="My Article"    @blocks= [          {:title=>'Block One', :content=>'content one'},          {:title=>'Block Two', :content=>'content two'},         ...

Writing Prettier Ruby Code with instance_eval

instance_eval is a method of ruby Object class that lets you pass a string of ruby code or a block to be evaluation in the scope of that object. Through instance_eval you can actually add new methods to a particular object. This can help you write more pretty and readable code. for example, we can define a new average method for an array of numbers >> a = [1, 2, 3, 4, 5, 6, 7, 8] => [1, 2, 3, 4, 5, 6, 7, 8] >> a.sum => 36 >> a.size => 8 >> a.average NoMethodError: undefined method `average' for [1, 2, 3, 4, 5, 6, 7, 8]:Array     from (irb):14 >> a.sum / a.size => 4 >> With instance_eval we can add that new method here a.instance_eval do     def average ; sum/size end end >> a.average => 4 In a lot of cases this approach can be useful.

REXML Error when running rcov

Rcov is one of the tools we use at eSpace to measure automated tests coverage for our rails applications. By rcov through gem install rcov , I had rcov(0.8.1.2.0) installed. After upgrading to the latest ubuntu 8.10, I realized that the installed rcov is not compatible with ruby 1.8.7 that i have. Rcov failed at generating the html reports with this stacktrace /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:131:in `[]': no implicit conversion from nil to integer (TypeError)     from /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:131:in `wrap'     from /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:131:in `wrap'     from /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:90:in `write_text'     from /usr/lib/ruby/1.8/rexml/formatters/default.rb:50:in `write'     from /usr/lib/ruby/1.8/rexml/formatters/pretty.rb:75:in `write_element'     from /usr/lib/ruby/1.8/rexml/formatters/prett...

Creating Functional Tests for Restful Authencation based websites

At eSpace we stress on having automated tests to cover our code. If you are using Restful Authentication plugin in your rails application, You will have to bypass the authentication filter that is processed before your controller actions.Restful authentication helps you does that through the login_as() method of the AuthenticatedTestHelper module. Let me show you how. In your functional test, login before you send the request.    class PublicationsControllerTest        fixtures :users        def test_method           login_as (:user_one)           Your test code...        end    end That user is loaded from the users fixtures and will act as the current logged in user. The fixtures must have something like: user_one:     id: 1     login...

has_and_belongs_to_many & Duplicate entry Error

In one of my Rails projects at eSpace , i had a Many to Many association between two models. this allowed me to look more deeply into the has_and_belongs_to_many association. When you say: class Question    has_and_belongs_to_many :surveys end class Survey    has_and_belongs_to_many :questions end Rails will assume there is a table questions_surveys that include foreign keys to the two entities. So this table should be created by the following migration create_table :questions_surveys,:id => false do |t|     t.integer :question_id, :null => false     t.integer :survey_id, :null => false end :id=>false will prevent the creation of the default primary key for that table.This is very important for has_and_belongs_to_many associations. as the API documentation say; other attributes in thatrelation will be loaded with the objects and will be read only, including the :id. So failing to disable the id generationfor that table wi...

Adding files to svn:ignore through command line

Recently, I have been making most SVN operations through command line. I find it more powerful, robust and surprisingly easy to use in basic operations. svn update svn status svn commit file1 file2 file3 -m 'message describing the updates' One of the things that i couldn't do at first is to mark items to be ignored. I finally realized how to do this.a directory under version control has several properties that can be set to describe the files in that directory. our property of concern is svn:ignore. We can ignore files by editing the svn:ignore property of the containing directory. svn propedit svn:ignore . This will open an editor allowing you to write the names of the files to be ignored. then saving and committing the directory. Note that the last parameter "." means the working directory. You can set you favourite text editor to be used in this operation by setting the environment variale export SVN_EDITOR=vim #OR export SVN_EDITOR=gedit You can find more deta...

Building interactive web pages using AJAX and RJS

When we talk about interactive web applications, AJAX is one of the most important technologies. Rails provides an easy way to use Ajax in your web site through RJS. RJS helps you make and ajax request and have several page elements updated correspondingly. Implementing Ajax requests using RJS is a simple matter of doing the following: Include the requried javascript files. Use the right rails tags to launch an ajax request. Declare that your action in the controller can respond to rjs. Define the rjs response (the changes to the DOM). Any rails application has the javascript files (prototype.js, effects.js, dragdrop.js, controls.js, application.js) by default. All you need to do is to include them in your layout file by adding: Rails provide 'remote' alternatives for its link and form tags link_to_remote vs link_to form_remote_for vs form_for and form_remote_tag vs form_tag. Using the remote alternative will cause the html tags generated to have a new AjaxRequest call; so whe...