Friday, 4 November 2016

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| a + b }
> proc2.call(2)      # call with only one parameter
TypeError: nil can’t be coerced into Fixnum


 2. Proc returns from the calling method, while Lambda doesn’t
> def proc
>   Proc.new { return ‘return from proc’ }.call
>   return ‘return from method’
> end

> puts proc
return from proc

> def lambda
>   lambda { return ‘return from lambda’ }.call
>   return ‘return from method’
> end

> puts lambda
return from method

If they are not called inside a method,
 
> proc1 = Proc.new { return “Thank you” }

> proc1.call
 
LocalJumpError: unexpected return


> lambda1 = lambda { return “Thank you” }
 

> lambda1.call
=> “Thank you”

Tuesday, 5 July 2016

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


Thursday, 30 June 2016

Elasticsearch error – failed to connect to master




Elastic Search Version : 1.4.4
Device : Mac OS

When ever I start elastic search, sometimes I'll get an error like this

  • failed to connect to master [[Sea Urchin][OCeACmvgQsqw-uRUq25UFA][Srinivasan.local][inet[/192.168.1.12:9300]]], retrying...
 Eventually I found the root cause of this issue. As I’m running elasticsearch on a single server ( development environment ), I had to do only one thing

Go to config/elasticsearch.yml and enable

discovery.zen.ping.multicast.enabled: false

Monday, 18 May 2015

Rails 4 WITH NO Database



Sometimes, you may want to create a Rails application without ActiveRecord or a database. There might be situations like, Your application only store data using third party APIs, or storing in to files or might not have any persistent data at all. Since Rails stands for the common case (database backed application), by default it fails to start the server without a database connection.

You can simply build a Rails app without a database:
  1. Comment / Remove any database gems ( mysql, mysql2, sqlite, pg, mongoid, etc..) from your Gemfile and then run bundle. 
     
  2. Open your application.rb. By default, in Rails 4.0 you can see one line which requires all of Rails:
    require 'rails/all'
    This includes ActiveRecord also, and requires a database connection. Instead, you can include the specific parts of Rails that you gonna use.
    See below:
    require 'action_controller/railtie'
    require 'action_mailer/railtie'
    require 'active_model/railtie'
    require 'sprockets/railtie'
    require 'rails/test_unit/railtie'
     
  3. The default app generated by Rails 4.0 has one reference to active record which must be removed, in config/environments/development.rb:
    config.active_record.migration_error = :page_load 
    4. Remove any references to active_record in your configuration 
Now run your application with out any backend :)

Tuesday, 5 May 2015

Conference Track Management - Ruby solution

I got to solve this problem as the part of an interview process.
It wont look good if I paste the whole code here.

So just sharing the link to my gitbub account. Conference Track Management

Wednesday, 22 April 2015

Binary Search implementation in Ruby

  1. def binary_search(array, value, from=0, to=nil)
  2.     to = array.count - 1 unless to
  3.     mid = (from + to) / 2
  4.     if value < array[mid]
  5.       return binary_search(array, value, from, mid - 1)
  6.     elsif value > array[mid]
  7.       return binary_search(array, value, mid + 1, to)
  8.     else
  9.       return mid
  10.     end
  11. end

puts binary_search([1,2,3,4,5,7,8,9,10,12,15,16,17,18], 15)

Program to perform divison operation of two numbers without using /, %, and modules operator


def devide(divisor, dividend)
  quotient = 0
  until(dividend < divisor) do
    dividend -= divisor
    quotient += 1
  end
  quotient
end

puts devide(15, 100)