Friday, March 16, 2018

How to install rbenv and Ruby build


rbenv use to setup different ruby environment based on your application, it's a nifty tool for development environment especially if your working different Ruby applications.


Installing instruction for rbenv
$ brew install rbenv
Add rbenv's bin path into .rc(.bash_profile) file
export PATH="$HOME/.rbenv/bin:$PATH" 
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
Make sure you have reloaded terminal with env changes

Installing specific Ruby version Ex. 2.3.4
$ rbenv install 2.3.4

ruby-build: use openssl from homebrew
Downloading ruby-2.3.4.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.3.4.tar.bz2


Installing ruby-2.3.4...


Installed ruby-2.3.4 to /Users/home/.rbenv/versions/2.3.4

Make your Ruby 2.3.4 as global 
$ rbenv global 2.3.4
$ ruby -v
ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-darwin17]
Sets the global version of Ruby to be used in all shells by writing the version name to the ~/.rbenv/version file. This version can be overridden by an application-specific .ruby-version file, or by setting the RBENV_VERSION environment variable.

Install ZSH and Oh My Zsh on your Mac

In order to Install zsh on your mac, make sure you've installed Homebrew on your machine. Homebrew is a package manger for macOS and very handy tool to install any packages on your mac.

If you haven't installed Homebrew, have a look http://brew.sh and follow the install instructions.

Installing ZSH

$ brew install zsh
This command will install zsh on your machine

Oh My Zsh


It's a framework for managing your ZSH configuration and more than 200+ plugins. would recommend to try this.Visit http://ohmyz.sh and follow the install instructions
$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
You can install any kind of plugins such as github

Monday, February 26, 2018

Design Patterns - Singleton

Singleton pattern is frequently used to provide global access point for a service. It restricts instantiation of a class to only one instance that globally available.

It's useful in your application when you need global instance which can be accessible in different parts of the application. Most common use case of this pattern is Application logging and configuration functionality.

Singleton pattern in Ruby

Class Logger
def initialize
@log = File.open("/tmp/tmp.log", "a")
end

@@instance = Logger.new

def self.instance
@@instance
end

def log(message)
@log.puts(message)
end

private_method :new
end


Logger.instance.log("Log me")

In this example, we created an instance of the "Class Logger" which can be accessed by class method "Logger.instance", so when you want to log any errors/warning/notice on your app, you can simply use "log" instance method which just opened a log file and appending message on that file. 

 At the end of the class we made method "new" as private, so that anywhere in the application we can't create new instance of Class "Logger"

Friday, January 19, 2018

Ruby PRY

Pry is one of the great tool for debugging ruby based application. In this blog would like to show some of the cool features of the Pry.


Ruby - Array#bsearch

Array bsearch it's very handy and one of the fastest way to find a value from the array.

Let's do benchmark analysis, based on the result, bsearch more powerful than using array find method. 
require 'benchmark'

array_data = (0..100_000_000)

Benchmark.bm do |x|
x.report(:find) { array_data.find {|number| number > 50_000_000 } }
x.report(:bsearch) { array_data.bsearch {|number| number > 50_000_000 } }
end

user system total real
find 3.160000 0.000000 3.160000 ( 3.174570)
bsearch 0.000000 0.000000 0.000000 ( 0.000011)

It can find a match with only O(log n) complexity.