An Example for the difference between class and instance variable in Ruby

Create a ruby class called Test

class Test
   def cv=(value)
     @@cv = value
     end
   def cb=(value)
     @cb = value
     end
   def cv
     @@cv
     end
   def cb
     @cb
     end
   end
 => nil 

Then create an instance of this class a and b

Try accessing the class variable without initializing it.

a = Test.new
 => # 
 a.cv
NameError: uninitialized class variable @@cv in Test
    from (irb):9:in `cv'
    from (irb):16

Initialize the class variable with some data. And add values to the instance variable of a and b.

a.cv=45
 => 45 

a.cv
 => 45 
b = Test.new
 => # 
b.cv
 => 45 
a.cb = 34
 => 34 
a.cb
 => 34 
b.cb
 => nil 
b.cb = 90
 => 90 
b.cb
 => 90 
Advertisements

Rails way of creating a new full url with new parameters after removing old parameters from a url

If we have some keys and values and we just need to create a url with this data just pass the url and hash as a parameter to the following method.

def generate_url_with_params(url, params = {})
    uri = URI(url)
    uri.query = params.to_query
    uri.to_s
end

If you want to get rid of the parameters from a url use the following method.

  def generate_url_without_params(url, params = {})
    uri = URI(url)
    full_params = Rack::Utils.parse_query uri.query
    params.each do |key, val|
      full_params.delete key
    end
    uri.query = full_params.to_param
    uri.to_s
  end

How to implement a simple captcha in Rails using gem ‘simple_captcha’

Here it is, a simple captcha for Rails applications.

To set up, add the following line to your Gemfile

gem "galetahub-simple_captcha", :require => "simple_captcha"

Run rails generator

rails generate simple_captcha

Do db Migration

rake db:migrate -t

In your view file add

%= show_simple_captcha %

Add the following in yml file
en.yml:

en:
  simple_captcha:
    label: "Enter the above code"
    placeholder: "Enter here"

In controller just include captcha helpers module

class ApplicationController < ActionController::Base
  include SimpleCaptcha::ControllerHelpers
end

Use this code in the controller for the captcha validation

if simple_captcha_valid?
  do this
else
  do that
end

Details about pg gem error: Can’t find the ‘libpq-fe.h header when doing bundle in Rails

If you are getting errors like:

installing pdf-reader 1.3.3
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

/home/vagrant/.rvm/rubies/ruby-2.2.0/bin/ruby -r ./siteconf20150306-5919-1rr483p.rb extconf.rb
checking for pg_config... yes
Using config values from /usr/bin/pg_config
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.

when trying to install pg gem,
you are just missing some dependancy library related to libpq. Install the following package


$ sudo apt-get install libpq-dev

then do


$ gem install pg -v 'version-no'

then do


bundle install

How to reinstall mongodb in ubuntu linux

Before reinstalling mongodb, in your linux system check whats installed in the system


$ sudo dpkg -l | grep mongo
ii mongodb-org 2.6.3 amd64 MongoDB open source document-oriented database system (metapackage)
ii mongodb-org-mongos 2.6.3 amd64 MongoDB sharded cluster query router
ii mongodb-org-server 2.6.3 amd64 MongoDB database server
ii mongodb-org-shell 2.6.3 amd64 MongoDB shell client
ii mongodb-org-tools 2.6.3 amd64 MongoDB tools

Remove all

$ sudo apt-get remove mongodb*

Install mongodb again, check mongodb org


$ sudo apt-get install -y mongodb-org

For particular version

$ sudo apt-get install -y mongodb-org=2.6.5 mongodb-org-server=2.6.5 mongodb-org-shell=2.6.5 mongodb-org-mongos=2.6.5 mongodb-org-tools=2.6.5

check whats newly installed

$ sudo dpkg -l | grep mongo
ii mongodb-org 2.6.8 amd64 MongoDB open source document-oriented database system (metapackage)
ii mongodb-org-mongos 2.6.8 amd64 MongoDB sharded cluster query router
ii mongodb-org-server 2.6.8 amd64 MongoDB database server
ii mongodb-org-shell 2.6.8 amd64 MongoDB shell client
ii mongodb-org-tools 2.6.8 amd64 MongoDB tools

check mongodb server status


$ sudo service mongod status

Start mongodb server

$ sudo service mongod start

Start mongo shell

$ mongo

check mongodb log file here

$ tail -n 200 /var/log/mongodb/mongod.log