Thursday, 12 December 2013

invalid byte sequence in UTF-8

Often we come across this #FiveWordTechHorror "invalid byte sequence in UTF-8" while working on Ruby projects or projects built on Ruby Framework (Ex : Rails).

This error occurs when we try to decode any string which has foreign characters (not plain English) which are submitted either through Javascript or say form using Rails and are not properly encoded .

So, the question is how to properly encode & decode ?

1) When submitted through Javascript :-
Use encodeURI or encodeURI() or encodeURIComponent() method to encode.
Ex : Suppose name contains unicode characters.
       encoded_name = encodeURI(name)

On ruby side decode it using CGI::unescape method.
Ex: Suppose encoded_name in params is received as params[:encoded_name]
      name = CGI::unescape(param[:encoded_name])

2) When submitted through Rails form :-
i) Use CGI::escape method to encode it and CGI::unescape method to decode it.
   Ex : encoded_name = CGI::escape(name)
          name = CGI::unescape(param[:encoded_name])


ii) Use unpack method of String class and then pack it using pack method of Array class.
     Ex : unpacked_name = name.unpack('U*')
            #This above statement return array of integers which are Base10 equivalent of  each characters hex code.
            name = unpacked_name.pack('U*')

Monday, 3 June 2013

include vs extend in Ruby

    include : Mixes in specified module methods as instance methods in the target class
    extend : Mixes in specified module methods as class methods in the target class
   Example :-
    module ReusableModule
      def module_method
        puts "Module Method: Hi there!"

    class ClassThatIncludes
      include ReusableModule
    class ClassThatExtends
      extend ReusableModule

    puts "Include"
    => "Module Method: Hi there!"

    puts "Extend"
    => "Module Method: Hi there!"
    If you include module ReusableModule in class ClassThatIncludes, the methods, constants, classes, submodules, and other declarations gets referenced.

    include is a private method, because it's intended to be called from within the container class/module.

    If you extend class ClassThatExtends with module ReusableModule, then the methods and constants gets copied. Obviously, if you are not careful, you can waste a lot of memory by dynamically duplicating definitions.

    extend is a public method.

    extend - adds the specified module's methods and constants to the target's metaclass (i.e. the singleton class. For ex :

    * if you call Klazz.extend(Mod), now Klazz has Mod's methods (as class methods)
    * if you call obj.extend(Mod), now obj has Mod's methods (as instance methods), but no other instance of of obj.class has those methods.
    extend is a public method

Tuesday, 15 January 2013

Displaying version of Ruby & Rails which your Rails application is using.

There was a requirement in the Rails project I was working on, to display application's technical information like Ruby version & Rails version which the application is using in an "Application Info" page in the production servers which was having many RoR applications running on different Rail's version.

So, to do the above task while searching a solution for it, I came across few constants provided by Ruby Language & Rails Framework with the help of which the above task can be done easily and it will be valid even if we upgrade the version of Ruby or Rails.

1) Displaying Ruby Version :-

Module in Ruby provides several useful constants with the help of which detailed Ruby version can be displayed.

To see all the constants provided by Module enter the below command in irb or rails console :


To see the list of constants helpful in determining the ruby version of an application, enter the below command :

Module.constants.collect{|c| c if c.to_s.include?('RUBY')}.compact

All these constants are pretty much self explanatory :


 2) Displaying Rails version :-

Module Rails itself provide constants with the help of which Rails version for an application can be determined easily.

To see all the constants provided by Rails Module enter the below command in rails console :


Out of all the constants VERSION can be used to do the task. VERSION is a sub-module inside Rails module. To list the constants of VERSION module, enter the below command :


These constants as well are pretty much self explanatory :


 You can use these constants directly in your application to display the required information .

Sunday, 16 December 2012

Failed to build gem native extension error while installing mysql & pg gem on ubuntu

If you are facing "Failed to build gem native extension" error while installing mysql and pg gem on ubuntu, which might look like

then you need to install the below mentioned packages for the same :

For mysql gem :

sudo apt-get install libmysqlclient-dev

For pg gem : 

sudo apt-get install libpq-dev

After installing the above packages, try installing the gem again. Hopefully, this time, it will get installed successfully.