Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Wednesday, June 19, 2013

Prepending in a Ruby Array

Appending in Ruby array is done in the following way :

my_array = ['s','a','u','r','a'] => ["s", "a", "u", "r", "a"]
my_array << 'v' => ["s", "a", "u", "r", "a", "v"]

Now, if you want to prepend any value to an array, Ruby's unshift method can be used for this purpose.

my_array => ["s", "a", "u", "r", "a", "v"]
my_array.unshift('c') => ["c", "s", "a", "u", "r", "a", "v"]

Thats it !

Monday, June 10, 2013

Ruby's try()

I came to know about Ruby's try() method today, liked, the overall purpose of this method and so, sharing a brief note about how we can use this method while writing codes. 

Many a times, we have to write conditions for checking for nil values/objects. try() can help us to write clean codes whereby we don't have explicitly do testing for nil value. 

Have a look at the small example below 

Without try():
puts @student.school.name if @student.school
With try():

puts @student.school.try(:name)

Read this http://apidock.com/rails/Object/try link, to know about try() method more in details.


Sunday, July 15, 2012

Generating Excel file with 'writeexcel' gem & 'axlsx' gem

Well, here I am describing a very short example of creating excel files in ruby. For this example, I have used the gem "writeexcel" writeexcel.

Here is a very small example

Well, Now a small example for generating excel file with axlsx gem.


Thursday, February 16, 2012

RVM - No same user error fix

Hi All, 

Today, after installing RVM in my system i was getting a strange error "no same user error" whenever I was trying to install ruby with rvm. I was getting error of similar sort: 
"Error opening archive: Failed to open '--no-same-owner': No such file or directory"
It is actually a bug in rvm, see this commit: 
https://github.com/wayneeseguin/rvm/commit/52018750763d5321b7b993e201c8589b98e090f9 
Finally after some googling, I could solve the issue with the following command:
[administrator@bdsi989-linux administrator]$ rvm get head
Hope this helps some one :)

Thursday, November 10, 2011

Rails, SQL Server 2008 & Windows - Tips and Tricks !!

In this post, I will be discussing in short about the configuration one need's to do for Rails, SQL Server 2008 and Windows XP system.


My configuration in my current system is:


1. Ruby: ruby 1.9.1p429 (2010-07-02 revision 28523) [i386-mingw32]
2. Rails 2.3.8 
3. OS: Windows XP.
4. SQL Server 2008


Note, that I have downloaded Ruby from rubyinstaller site, the link is mentioned below in this post.
First of all, we need two gems to be installed in our system to work with SQL Server 2008


1. activerecord-sqlserver-adapter, command for installation: gem install activerecord-sqlserver-adapter -v=2.3.6


2. ruby-odbc, command for installation: gem install ruby-odbc


Please note, without these two gems thing will not work properly.


activerecord-sqlserver-adapter has other higher versions, which i presume works with rails 3.0 + versions. For my purpose, I have used the version, 2.3.6
Before installing, ruby-odbc gem, you will need to install the ruby development kit from the ruby installer site. The links for rubyinstaller site are:


1. http://rubyinstaller.org/
2. http://github.com/downloads/oneclick/rubyinstaller/DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe
3. https://github.com/oneclick/rubyinstaller/wiki/Development-Kit (installation instructions can be found out here)


Without installing the devkit, the ruby-odbc gem will not be able to install.


Now, in the next step, we will have to create  dsn link for odbc connection.You can get a idea about to create it by going through this link: http://www.ehow.com/how_4642566_setup-odbc-database-connection.html


Once, the dsn link is done and lets assume that you have named it as dsn_link, you have to enter the information in your database.yml file. Here is a snapshot of my database.yml


development:
  adapter: sqlserver
  mode: odbc
  dsn: dsn_link
  username: my_username
  password: my_password



This is it !!. Restart your development server and now your application will be pointing to the SQL server configuration :)

Friday, October 28, 2011

Gem.source_index is deprecated, use Specification ( ERROR in Rails)

Hi,


Today, while setting up my new system with Rails, I was getting the following error when starting mongrel/webrick dev server:




NOTE: Gem.source_index is deprecated, use Specification. It will be removed on or after 2011-11-01.
Gem.source_index called from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rails-2.3.8/lib/rails/gem_dependency.rb:21.
NOTE: Gem::SourceIndex#refresh! is deprecated with no replacement. It will be removed on or after 2011-11-01.
Gem::SourceIndex#refresh! called from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rails-2.3.8/lib/rails/vendor_gem_source_index.rb:34.
NOTE: Gem::SourceIndex#load_gems_in is deprecated with no replacement. It will be removed on or after 2011-11-01.
Gem::SourceIndex#load_gems_in called from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:322.
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:127.
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:127.
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:127.
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.
Gem::SourceIndex#add_spec called from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/source_index.rb:127.
NOTE: Gem::SourceIndex#add_spec is deprecated, use Specification.add_spec. It will be removed on or after 2011-11-01.


My configuration for rails was:


Ruby version 1.9.2
Rails version 2.3.8
gems version 1.8.11


OS version: Windows XP.


I solved the problem very easily. If you notice in the configuration section, my gem version is 1.8.11. This is causing the issue. So, I downgraded my gem version to 1.6.2 and the problem was SOLVED !!.


The following is the command for downgrading the gem version:


gem update --system 1.6.2


Hope that this small instruction will help someone !!







Thursday, September 15, 2011

Complete guide for setting up Starling

Complete guide for setting up starling. It includes installation, configuration and monitoring of the services 

Dynamically generating methods with the help of define_method in Ruby

Ruby language provides a very interesting feature with the help of which you can dynamically generate methods at run-time. Yes you have read it correctly, you can write codes which will generate methods for you :).


So how can you do it in ruby ? Well, Ruby provides a method called define_method. This method is a private class method


Here is the syntax :
define_method(symbol, method)
define_method(symbol) { block }

where,
  • symbol is the method name; it can be either a symbol, a string, or a variable whose value contains the method name.
  • method is a lambda or Proc object containing the method logic. 
Let me define a very simple method, which will print a hello message for the demonstration purpose. This example will generate an instance method, therefore we will have to create a object for printing the hello message


class D
  define_method(:hello) do |h|
    puts "Hello !!"
  end
end
C.new.hello('Jack')   => Hello Jack !!



Note that, this is a very simple example. Here I wanted to describe the basic idea of define_method provided in Ruby.

Managing multiple version of Ruby and Rails in Windows: PIK

Here I will give a breif description about a tool called Pik (https://github.com/vertiginous/pik), a ruby version manager for Windows.


With the help of Pik, one can have multiple version of Ruby and Rails in his system. Find below the steps that are required for installing Pik:


1. Go to RubyForge and download Ruby. If Ruby is already present, then skip step 1.
2. For installing pik, run the following command from your terminal. gem install pik. You should see ‘Successfully installed pik-0.2.8‘ message soon enough
3. After downloading this gem, you will need to intall pik in some directory other than "C:\Ruby\bin". You can perform the following steps:
    a. d:
    b. mkdir pik
    c. Include the newly created directory, i.e D:\pik in the PATH environment variable.
    d. pik_install D:\pik
Pik will be now installed in your system.



Using Pik


I can now start using Pik for mantaining multiple versions of Ruby and Rails !!.


>pik
** Adding:  187: ruby 1.8.7 (2011-06-30 patchlevel 352) [i386-mingw32]
Located at:  C:\Ruby187\bin
Usage: pik command [options]

Executing pik list outputs all the ruby installations it knows about. 



>pik list
* 187: ruby 1.8.7 (2011-06-30 patchlevel 352) [i386-mingw32]
  192: ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
 
As Rails 3.0 requires 1.8.7 or higher, let’s install the latest version of Ruby.



You can then switch to the particular version you want, for example originally I was running 1.8.7.


>ruby -v
ruby 1.8.7 (2011-06-30 patchlevel 352) [i386-mingw32]

But I can then switch to 1.9.2 with a simple command.



>pik switch 192

>ruby -v
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]


Now by switching the versions of ruby we can install and have different rails version. For example with ruby version 1.8.7, I can have rails 2.3.8 version and I can have rails 3.0 version by switching the ruby version to 1.9.2 !!