Posts

Ruby 2.5 Major Changes

1. rescue/else/ensure are allowed inside do/end blocks without begin/end2. Print backtrace and error message in reverse order (experimental)3. Kernel#yield_self           2.yield_self { |n| n * 10 } #=> 20
          names = ['Alice', 'Bob'] 
          names.join(', ').yield_self { |s| "(#{s})" }
          #=> "(Alice, Bob)"

4. String#delete_prefix/delete_suffix5. Array#prepend/append as aliases of unshift/push6. Hash#transform_keys/transform_keys!

Prime Number with out Prime Class

a = [1,4,5,12,9,5,3,5]

class Array
  def prime_numbers
    self.select(&:prime?)
  end
end

class Integer
  def prime?
    return if self <= 1
    (2..Math.sqrt(self).ceil).none? { |i| (self % i).zero? }
  end
end

 a.prime_numbers
=> [5, 5, 3, 5]

Ruby's class_eval & instance_eval. Is it that confusing?

Image
We can do class_eval & instance_eval using following syntax,
In this example, class_eval? method can be accessed my any objects of the String class. Where as instance_eval? method can be accessed by only String class. 

More clearly, class_eval creates instance methods, but instance_eval creates class methods or it can be applied to only one object at a time.

[3] pry(main)> String.class_eval?
NoMethodError: undefined method `class_eval?' for String:Class
Did you mean?  class_eval
from (pry):3:in `<main>'
[4] pry(main)> String.instance_eval?
=> true
[5] pry(main)> String.new.class_eval?
=> true

my_string = "This is a string"

For example you can simply do like below as any string is an object of String Class
[5] pry(main)> my_string.class_eval?
=> true

But you can not do 
[7] pry(main)> my_string.instance_eval?
NoMethodError: undefined method `instance_eval?' for "This is a string":String





Install Nginx & passenger in CentOS 6.x

Nginx Installation First of all, nginx is not a repo available under yum. But you can install epel, through which install nginx.

1. Install EPEL
yum install epel-release

OR

download http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo rpm -iUvh epel-release-6-8.noarch.rpm

2. Check EPEL repo
yum repolist

You should get

HPIT-v2cloud-RedHat-nover-x86_64                  HPIT-v2cloud-RedHat-nover-x86_64  0
HPIT-v2cloud-nodist-nover-noarch                      HPIT-v2cloud-nodist-nover-noarch   22
HPIT-v2cloud-nodist-nover-x86_64                     HPIT-v2cloud-nodist-nover-x86_64    3
RedHat-6.5Server-x86_64-Server                        RedHat Linux 6.5Server - os - x86_64 - Server
RedHat-updates-QPK-6Server-x86_64                RedHat-updates-QPK-6Server-x86_64
centos-gluster37                                                    CentOS-6Server - Gluster 3.7
*epel                                                                     Extra Packages for Enter…

Installation guide for RVM Multiuser

When I was trying to install RVM for multiuser, There was no proper guidelines for it. So I decided to create one. Please follow the below steps to install RVM for multiuser.

1. Install mpapis public key
sudo gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
or
curl -sSL https://rvm.io/mpapis.asc | sudo gpg2 --import
2. Install RVM for Multiuser
curl -L https://get.rvm.io | sudo bash -s stable

3. Load RVM to Environment

echo "[[ -s /usr/local/rvm/scripts/rvm ]] && source /usr/local/rvm/scripts/rvm" >> ~/.bashrc  source .bashrc

4. Reload shell configuration & test
Close out your current shell or terminal session and open a new one (preferred). You may load RVM with the following command:
source /usr/local/rvm/scripts/rvm

5. Final Testing
If installation and configuration were successful, RVM should now load whenever you open a new shell. This can be tested by executing the following command which should output rvm is a f…

Ruby Proc & Lamda - A close look

One of the core concepts about Ruby that a programmer should remember is that blocks are not objects. It is possible to convert them into objects by wrapping them inside the “Proc” class instance. The “Proc” and “Lambda” classes are probably the most awesome aspects of Ruby and need to be used well in order to get the desired results. Not much of knowledge is available on how to use Procs and Lambdas and what the differences between both are. I’d like to take a stab at how we can differentiate between Procs and Lambdas with a couple of thoughts below.
1. Proc doesn’t check the parameters passed, but Lambda does
> proc1 = Proc.new { |a, b| a + 5 }
> proc1.call(2)      # call with only one parameter
=> 7                    # no error, unless the block uses the 2nd parameter


> lambda1 = lambda { |a, b| a + 5 }
> lambda1.call(2)
ArgumentError: wrong number of arguments (1 for 2)


Proc will throw error only if the block uses the second param.
> proc2 = Proc.new { |a, b| …

Rails 4, MySQL, and Emoji (Mysql2::Error: Incorrect string value: '\xF0\x9F\x8C\x9D')

Step 1

Change encoding from utf8 to utf8mb4
class ConvertTableToUtf8mb4 < ActiveRecord::Migration
  def change
    # For the table that will store unicode execute:
    execute "ALTER TABLE `table_name` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
    # For each column with unicode content execute:
    execute "ALTER TABLE `table_name` CHANGE column_name VARCHAR(226) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
  end
end
Step 2

Change database.yml according to the changes made.

development:
  adapter: mysql2
  database: database_name
  username: username
  password: password
  encoding: utf8mb4
  collation: utf8mb4_unicode_ci