Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Tuesday, 10 March 2020

Multiple Vms Vagrantfile

Multiple Vms Vagrantfile

Multiple Vms Vagrantfile

2020-03-10T17:50:49Z



Introduction

A Vagrantfile for multiple VMS.

Prepare Environment

mkdir Vagrant_VMS
cd Vagrant_VMS
vagrant init

Populate Vagrantfile with following contents.

all_hosts = [
    {
        vagrant_hostname: "amachine",
        full_hostname: "amachine.virtual.machine",
        vmbox: "fedora/31-cloud-base",
        vmbox_version: "31.20191023.0",
        ip: "10.0.0.10",
        memory: 2048,
        cpus: 1
    },
#    {
#        vagrant_hostname: "anotherMachine",
#        full_hostname: "another.virtual.machine",
#        vmbox: "fedora/31-cloud-base",
#        vmbox_version: "31.20191023.0",
#        ip: "10.0.0.12",
#        memory: 2048,
#        cpus: 1
#    },
]

# individual machine names must be mentioned is below command line in
# order to bring machines. (due to autostart: false)
# vagrant up amachine anotherMachine
Vagrant.configure("2") do |config|
    #config.vm.box = "fedora/31-cloud-base"
    #config.vm.box_version = "31.20191023.0"

    all_hosts.each do |host|
        config.vm.define host[:vagrant_hostname], autostart: false do |this_host|
            this_host.vm.network :private_network, ip: host[:ip]
            this_host.vm.hostname = host[:full_hostname]
            this_host.vm.box = host[:vmbox]
            this_host.vm.box_version = host[:vmbox_version]

            this_host.vm.provider "virtualbox" do |m|
                m.memory = host[:memory]
                m.cpus = host[:cpus]
            end
        end
    end
    config.vm.synced_folder ".", "/vagrant", type: "nfs"
end

Start VMs

vagrant up amachine

Note: You need to mention vm name in vagrant up command due to presense of autostart: false in above Vagrantfile.

Monday, 14 March 2016

Pretty JSON print of a CouchDB document

Using Python:
$ curl --silent --insecure https://user:'password'@couchdb-server:5984/database/document | python -m json.tool
Using Ruby:
$ gem install json
$ curl --silent --insecure https://user:'password'@couchdb-server:5984/database/document | ruby -rjson -n -e 'str=JSON.parse $_ ;puts JSON.pretty_generate(str)'

$ curl --silent --insecure https://user:'password'@couchdb-server:5984/database/document | ruby -n -e 'require "json" ; str=JSON.parse $_ ;puts JSON.pretty_generate(str)'

Ruby Procs and Blocks relation

#! /usr/bin/env ruby
def amethod(arg=10)
  yield 2
  arg * 3
end

def bmethod(&block)
  block.call
end

def cmethod(block)
  block.call
end

p = Proc.new { puts "I am a proc object" }





puts "Below is the output when amethod is called as ---> str = amethod(4) { |num| puts num }"
str = amethod(4) { |num| puts num}
puts "str = amethod(4) { |num| puts num  } returned ---> #{str}"

puts "====================="
puts "Below is the output when amethod is called as ---> str1 = amethod { |num| puts num } ; i.e no argument to amethod."
str1 = amethod { |num| puts num }
puts "str1 = amethod { |num| puts num  } returned ---> #{str1}"

puts "====================="
puts "Below is the output when amethod is called as ---> amethod { puts \"This output is from code block that does not have block arg.\" } ; i.e no argument to amethod,  no code block arg and return value discarded."
amethod { puts "This output is from code block that does not have block arg." }

puts "====================="
puts "Below is the output when amethod is called as ---> amethod(&p); where p = Proc.new { puts \"I am a proc object\"}"
amethod(&p)

puts "===================="
puts "Below is the output when amethod is called as ---> ret = amethod(20, &p); where p = Proc.new { puts \"I am a proc object\"}"
ret=amethod(20, &p)
puts "ret = amethod(20, &p) returned: #{ret}"

puts "===================="
puts "Below is the output when bmethod is called as ---> bmethod { puts \"This output is from code block that does not have block arg.\" }"
bmethod { puts "This output is from code block that does not have block arg." }

puts "==================="
puts "Below is the output when bmethod is called as ---> bmethod(&p); where p = Proc.new { puts \"I am a proc object\"}"
bmethod(&p)

puts "==================="
puts "below is the output when cmethod is called as ---> cmethod(p); where p = Proc.new { puts \"I am a proc object\"}"
cmethod(p)

Following is the output of above code:
Below is the output when amethod is called as ---> str = amethod(4) { |num| puts num }
2
str = amethod(4) { |num| puts num  } returned ---> 12
=====================
Below is the output when amethod is called as ---> str1 = amethod { |num| puts num } ; i.e no argument to amethod.
2
str1 = amethod { |num| puts num  } returned ---> 30
=====================
Below is the output when amethod is called as ---> amethod { puts "This output is from code block that does not have block arg." } ; i.e no argument to amethod,  no code block arg and return value discarded.
This output is from code block that does not have block arg.
=====================
Below is the output when amethod is called as ---> amethod(&p); where p = Proc.new { puts "I am a proc object"}
I am a proc object
====================
Below is the output when amethod is called as ---> ret = amethod(20, &p); where p = Proc.new { puts "I am a proc object"}
I am a proc object
ret = amethod(20, &p) returned: 60
====================
Below is the output when bmethod is called as ---> bmethod { puts "This output is from code block that does not have block arg." }
This output is from code block that does not have block arg.
===================
Below is the output when bmethod is called as ---> bmethod(&p); where p = Proc.new { puts "I am a proc object"}
I am a proc object
===================
below is the output when cmethod is called as ---> cmethod(p); where p = Proc.new { puts "I am a proc object"}
I am a proc object

Tuesday, 8 March 2016

Java and Ruby Inheritance Comparison

Following is a Ruby Program. This is heavily commented with reference to Java and Ruby both.
class A
  def methoda_public
    puts "Hello from methoda_public self=#{self}"
  end

  def methoda_private
    puts "Hello from methoda_private self=#{self}"
  end

  def methoda_protected
    puts "Hello from methoda_protected self=#{self}"
  end

    # 1) Java => class methods are defined using static keyword. instances can access class methods in Java.
    # 2) Ruby => class methods are actually singleton methods of class itself. (in Ruby a class is an object itself.). class methods are actually defined in
               # singleton class of "class object" and hence called as singleton methods. Only that object can call singleton method on which it is defined and none other.
               # Singleton methods are Exclusive to a single particular object only. Hence even the instances created from class A cannot access class methods.
               # derived class can invoke base class' singleton object. Again instances of Derived class cannot access singleton method defined on base class.
  def self.methoda_classmethod_public
    puts "hello from methoda_classmethod_public self=#{self}"
  end

  private :methoda_private
  protected :methoda_protected
end

class B < A
  def public_methodb_calling_private_methoda

      # 1) Java => private members are not inherited directly in derived class. So this will not work in Java.
                 # Derived class inherits base class' protected and public members. So if Base class' protected/public method access private member
                 # then Derived class can also 'work' on private member via inherited protected/public methods.
      # 2) Ruby => private members are inherited in Ruby. So this will work in ruby.
    methoda_private       

      # 1) Java => you cannot access private memebers directly in derived class. So this will not work in Java.
      # 2) Ruby => Will not work as private members must be accessed WITHOUT explicit receiver in derived class.
    #A.new.methoda_private  # A new object is created and we are trying to access its private member.  will not work.
    #self.methoda_private   # An equivalent in Java would be this.methoda_private(). But ofcourse in Java private members are not inherited so will not work.
      
      # 1) Java => Protected members can be accessed in derived class directly becuase they are inherited. So this will work.
      # 2) Ruby => Protected members are inherited in Ruby as well. So this will work.
    methoda_protected

      # 1) Java => protected memeber cannot be accessed in drived class using reference variable(i.e. by creating object).
      # 2) Ruby => This will work. But this will NOT work in Ruby also if below expression i.e. A.new.methoda_protected is called outside class B. 
    A.new.methoda_protected

      # 1) Java => A Java equivalent would be this.methoda_protected() . This will work in Java. As protected methods are inherited in Java directly. Needed verification.
      # 2) Ruby => This will work. Ruby will be calling inherited method. Java will also do the same.
    self.methoda_protected

      # 1) Java => Instance method can call static method (class method).
      # 2) Ruby => This will not work. Instance method CANNOT call class method.
    #methoda_classmethod_public

      # 1) Java => instance method can call static method (class method). In java it would be this.methoda_classmethod_public()
      # 2) Ruby => this will not work. Because inside instance method self will be referring to the instance of B. And instance of B CANNOT call
                 # singleton method of class A.
    #self.methoda_classmethod_public
  end

    # 1) Java => Both below statements are not allowed in Java. These are called outside any method definition.
    # 2) Ruby => Both below will work in Ruby. Hence class methods are inherited but only to be called by derived class itself.
               # class methods are singleton method. Normally singleton methods are called by only their own onjects.
               # But when in case of singleton methods defined on class (class methods), they can be called by dervied class also. (Not by derived class instances).
  methoda_classmethod_public
  self.methoda_classmethod_public
end

#A.new.methoda_private     # => will not work. (same as in Java)
#A.new.methoda_protected   # => will not work. (same as in Java). Compare and take a note of this expression inside class B as well.
#A.new.methoda_classmethod_public # => Will not work in Ruby but will work in Java. Java syntax would be: A a = new A(); a.methoda_classmethod_public() .

B.new.public_methodb_calling_private_methoda
B.methoda_classmethod_public  # => will work both in Java and Ruby

Wednesday, 2 December 2015

Find repeated Group pattern match. Case study of Ruby, Python and Go

Find repeated Group pattern match. Case study of Ruby, Python and Go

Sometimes I have to find the a group of patterns that are occurring in a log file. Normal grep -f <filename> does that job but it does not print the delimiter that can distinguish between repeated found patterns.

I needed something that can tell me that it has found a few patterns from a group in a sequence and last pattern has been found. Draw a delimiter here and start search for group again in remaining file.

I first wrote it in Ruby. I had huge files to parse. Ruby script was talking quite long to finish. As a case study I decided to write same utility using Python and Go.

Needless to say , Go was much faster.

Following are the codes.

Following are the sample data files.

pattern file :
hotmail
yahoo
google
gmail
Subject file:
This is hotmail test
But this is going to be a yahoo
Now is google
Another one is gmail
Junk lines
more junk lines
MORRRRRRRRRR
This is hotmail test
But this is going to be a yahoo
Another one is gmail
Now is google
Another one is gmail
But this is going to be a yahoo
This is hotmail test
Junk lines
more junk lines
MORRRRRRRRRR
This is hotmail test
But this is going to be a yahoo
Now is google
Another one is gmail
Junk lines
more junk lines
MORRRRRRRRRR
Following is the output:
$ go run atest.go pattern subjectfile
This is hotmail test
But this is going to be a yahoo
Now is google
Another one is gmail
===========
This is hotmail test
But this is going to be a yahoo
Another one is gmail
===========
Now is google
Another one is gmail
===========
But this is going to be a yahoo
This is hotmail test
This is hotmail test
But this is going to be a yahoo
Now is google
Another one is gmail
===========