Hi Guys,

In my previous article, Don’t hesitate to try out new things in Ruby on Rails, you got to know, how to install and configure pik to support multiple versions of Ruby on Windows.

In this article, I will add another simpler way to install pik on windows and configuring it for multiple versions of ruby. For Devkit, you can follow the same old article.

NOTE: I will go with Ruby192 and on that Ruby187 and so on… You can accomplish it in a reverse mode as well.

Step1: Download Ruby193 from here. Do install the RubyInstaller version i.e .exe file.

Step2: After downloading, Install it (click on that checkbox which will say, add Ruby to your environmental variables path).

Step3: Once the installation is done, open a command window and type:
ruby -v
It will tell you that, current Ruby is 1.9.3

Step4: Install the pik gem. Type:
gem install pik

Step5: Type- pik_install C:/tools

Step6: Add this path to your Environmental variables
My Computer > Right Click > Properties > Advanced System Settings > Environmental Variables > Path

Step7: Now, go to RubyInstaller.org and download the 7-zip archive of Ruby187 i.e Ruby 1.8.7-p358.

Step8: Once the download is finished, extract all the content of that file inside C:\ folder, using 7-zip utility. After extracting, rename the folder to Ruby187.

Step9: Open a new command window and type:
pik add C:\Ruby187\bin

Yuppy!! it will add 187 into the existing versions of Ruby. From here you can install ‘N’ number of Ruby versions on Windows.

Advantages of doing this:

If using Netbeans, you can now easily configure/add/manage multiple versions of Ruby.
If you follow the old approach, your pik version of Ruby won’t able to include the DevKit, due to which you may face problems, while installing Native extension gems. For e.g consider this path: C:/Users/foo bar/.pik/rubies/. It contains a space between foo and bar. So devkit skips this many of the times.
Happy coding…
Your feebback/suggestions are always welcome.

Learn Core Ruby Concepts

Hello Everyone,

We all know basic concepts of Ruby, like classes, objects, methods etc. etc and we integrate this with the concept of rails beautifully.

In short I can say we all are very good at Ruby on Rails, and rails specially, but when it comes to Ruby, we are not that much confident tough.

In this Post of mine, we will cover all the important concepts of Ruby, some pure ruby code, and how we can apply this with Rails.

So let’s start learning the Core Ruby Concepts.

  • In RoR programming, most of the times we need to identify the type of string. We don’t know if the incoming string is a “Integer”, “Float”, “String”. To check that:
    y = Incoming String
    y.is_a?(Integer) => returns true or false
    y.is_a?(String) => returns the same
    y.is_a?(Fixnum) => -----#-------
    y.is_a?(Float) => -----#-------

    • We can also ask the variable exactly what class of variable it is using the class method:
      y = "Incoming String"
      y.class => returns String
      y = 10.25
      y.class => returns Float
      y = 10 => returns Fixnum
    • Sometimes we need to change the incoming string to say Integer (Note: It is for sure that in RoR application, when browser sends any parameter to any controller’s method, it will be string only), Float for further operations. To do that:
      x = "10.25"
      y = x.to_f
      p y.class => returns Float
      z = y.to_i
      p z.class => returns Fixnum
  • Variable type: Ruby has four types of variables:
    • Local variable [a-z] or _
    • Global Variable $
    • Instance variable @
    • Class variable @@

    To identify the type of variable, do:
    a = 10
    p defined?(a) => "local-variable"
    $b = "Puneet"
    p defined?($b) => "global-variable"

  • Metaprogramming: Metaprogramming is a technique in which code writes other code. The prefix Meta refers to Abstraction.
    • At a high level, metaprogramming is useful in working towards DRY principle (Don’t Repeat Yourself).
    • Metaprogramming is primarily about simplicity. One of the easiest ways to get a feel for metaprogramming is to look for repeated code and factor it out. Redundant code can be factored into functions; redundant functions or patterns can often be factored out through the use of metaprogramming.

Minimise URL in Ruby

Does it looks odd?? You have so many websites which allows you to minimise your url like tinyurl, bit.ly etc etc list is endless, but what if you get some API of those to work with so you can minimise the url with your rails application or as a stand-alone ruby program??

Bit.ly comes with that. It provides ruby programmers an interface by which they can minimise the url. Wondering How? see it in action..

I am creating a simple ruby program here, if you want you can use it in your application…

All you need is json and open-uri to finish it off.

require ‘open-uri’
require ‘json’

apikey=’YOUR API KEY’
url = “http://api.bit.ly/shorten?version=#{version}&longUrl=#{code}&login=#{user}&apiKey=#{apikey}”
buffer = open(url, “UserAgent” => “Ruby-ExpandLink”).read
result = JSON.parse(buffer)
shorturl = result[‘results’]['shortUrl']

That is it. Run this program in console/command prompt, where ever you want to see the output.

Post your queries, suggestions