Posts

Showing posts with the label ruby

Ruby 2.5 Major Changes

1. rescue/else/ensure are allowed inside do/end blocks without begin/end 2. 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_suffix 5. Array#prepend/append as aliases of unshift/push 6. 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 ...

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 wh...

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

Binary Search implementation in Ruby

def binary_search(array, value, from=0, to=nil)     to = array.count - 1 unless to     mid = (from + to) / 2     if value < array[mid]       return binary_search(array, value, from, mid - 1)     elsif value > array[mid]       return binary_search(array, value, mid + 1, to)     else       return mid     end 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)

Ruby Count, Length & Size

These methods can be applied on Arrays, Hashes or Objects.  Here I'm just trying to illustrate the difference between count, length and size. Array#length Basically length method returns number of elements in the array. Example, suppose we have array a rr as, a rr = [1,2,3,4,5] Calling Array#length method on array will give result as, arr.length => 5 Array#size Array#size is just an alias to the Array#length method. This method executes same as that of Array#length method internally. Example, arr.size => 5 Array#count It has more functionalities than length or size. This method can be used for getting number of elements based on some condition . Unlike length/size, we can pass block or an argument to count. This can be called in three ways: Array#count => without condition, Returns number of elements in Array Array#count(n) => passing value, Returns number of elements having value n in Array Array#count{|i| i.zero?} =...