Tuesday 4 July 2023

Install old ruby on MacBook Pro m1/m2 chip using rbenv(Fresh installation)

Installing older ruby prior to 2.7.2 does not work easily on Mac m1/m2 chips. I wasted a lot of time in this.
I will share the steps which worked for me and it might save others time as well.

Make a fresh installation. First uninstall homebrew(brew) and rbenv and remove all the configurations settings that you might have done while trying to install ruby.
You might have homebrew in /opt/homebrew or in some other places like if you enabled rosetta mode.

Make sure to remove all the configurations settings as well in .bash_profile, .zprofile, .zshrc and lets start with a fresh installation.

 Here are the steps that you need to follow:

1) Enable Rosetta mode(This installs all the application using arch -x86_64):

  • Go to Applications => Utilities from finder.

  • Select Terminal.

  • Press CMD+I (capital i) for "Get Info" or right click on go to in

  • Check box for "Open using Rosetta"

  • You will be asked to install Rosetta, install it and restart your Mac.

    2) Install homebrew using this command:


    And follow the instructions in the end to configure homebrew.

    Run cat .zprofile and make sure you have this:

    eval "$(/usr/local/bin/brew shellenv)"


    3) Install rbenv using this command:

    brew install rbenv

    And configure rbenv so that it loads on start.

    Add this to .zshrc:

    eval "$(rbenv init - zsh)"



    4) Finally, Install ruby using this command(Restart your Mac and make sure brew and rbenv commands are working):
    CFLAGS="-Wno-error=implicit-function-declaration" rbenv install 2.1.7
    These are the steps which worked for me.





    Saturday 30 August 2014

    Multi-select dropdown with has many through association in rails

    Lets consider you have a brand and a brand can be in multiple states.
    And therefore to create a brand you want to select multiple states in which that brand exists.

    Brands and states are associated with a has_many through association.

    Here is your models:

    state model:

    class State < ActiveRecord::Base
      attr_accessible :name
       has_many :brand_states
      has_many :brands, through: :brand_states

    end

    brand model(note state_ids in attr_accessible):

    class Brand < ActiveRecord::Base
    attr_accessible :name,:state_ids     

    end

    brand_state model

    class BrandState < ActiveRecord::Base
      belongs_to :brand
      belongs_to :state
    end


    Your form for new and edit:

    <%= simple_form_for @brand, :html => { :class => 'form-horizontal', :multipart => true }  do |f| %>
      <%= f.input :name, :as => :string %>

        <div class="field ">
          <%= f.label :states, "Select States" %>
          <%= f.collection_select(:state_ids,State.all, :id, :name,{},:multiple => true) %>
        </div>  

    <% end %>

    Controllers:
    Everything will be same no changes for multiselect dropdown.

    And thats it  you are done!

    Sunday 11 August 2013

    Rabbitmq implementation in rails using amqp gem


    RabbitMQ is a message broker.It accepts messages from producers, and delivers them to consumers. In-between, it can route, buffer, and persist the messages according to rules we give it.
    We need Ruby's amqp gem to implement it in rails.

    For installation of rabbitmq and amqp gem check out the github link of amqp gem

    After installation,

    You need to create a ruby file in initializers and paste this code snippet:

    require "amqp"

    error_handler = Proc.new do |settings|
      puts "!!! Failed to connect..."
      EM.stop
    end
    puts "Creating a subscriber"
     Thread.new {
      EventMachine.run do
        $connection = AMQP.connect(:host => '127.0.0.1')
        puts "Initialize Subscriber..."
        $channel = AMQP::Channel.new($connection)
        $queue = $channel.queue("amqpgem.examples.helloworld", :auto_delete => true)
        $exchange = $channel.direct("")
        $queue.subscribe do |payload|
            puts "Received a message: #{payload}..."
            sleep(10)
            puts "After sleep"
        end
      end 
    }
    class MessageQueue

      def push(message)
        puts "Got a message for pushing"+message
        EventMachine.next_tick do
          #puts "Published to the routing key "+$exchange.to_yaml
          $exchange.publish(message, routing_key: "amqpgem.examples.helloworld")
        end
      end
    end

    MESSAGE_QUEUE = MessageQueue.new

    Now In order to push a message into the queue call this in the controller:

    MESSAGE_QUEUE.push("Pushing my first message")


    And now whenever you call the controller,  you will get a message into the console "After sleep" after around 10 seconds.

    Here is Little explanation of the code:

    Whenever we start our server, our ruby file gets loaded which in turn creates a thread and start EventMachine into it.

    EventMachine is an event-driven I/O and lightweight concurrency library for Ruby. It provides event-driven I/O using the Reactor pattern, much like JBoss, Netty, Apache MINA, Node.js.

     After EventMachine has been started we start a connection to AMQP broker(Rabbitmq). Then we create a channel on the connection and a queue on that channel.And then we are starting our subscriber and bind it to the queue. It will be responsible for taking the message and other information from the queue and process  accordingly as instructed.

    Now, whenever  we call the controller it calls
    MESSAGE_QUEUE.push and passes a message to the push method which in turn pushes  it to the queue. Then there,it will be taken by the subscriber and it will do rest of the work.

    For detailed explanation  checkout the github link of amqp gem

    Thursday 25 July 2013

    Parse formatted PDF in rails

    I had to parse a PDF which was formatted and it has different styles eg: bold texts. I tried pdf-reader gem but it wasn't parsing properly. Bold texts were repeated.And it was really hard to figure it out what was the original text.

    Then I tried  iText its implementation has been done in Java.Implemented it using ruby Java Bridge, but none of its strategy could parse my formatted pdf properly.

    Then at last I found Docsplit and It could parse my PDF properly.

    I will simply show you the steps:-

    These are the bunch of gems you need to include and one more thing you might need is SUDO permission in order to install some gems:-

    gem "pdftk"
    gem "docsplit"
    gem "glib2"
    gem "gdk_pixbuf2"
    gem "poppler"

    Some of the gem doesn't get installed simply through bundle install and you might need to install it using apt-get. Just google it if you are having any issue on installing any gem or you can leave a comment below.

    After successfully installing all the gems:

    You simply need this one line of code and it will parse your PDF and save all of its text to a text file with the same name as the PDF file.

    file_dest = Rails.public_path+'/pdfparser/text (where you want to save the text file)
    Docsplit.extract_text(pdf_path,:output =>file_dest)

    There are many other options provided,  like you can parse a specific page of the PDF or even extract images from the PDF please refer to its documentation.

     And after this if you want to find text between two texts from the file here's  what you need to do:

    text_main = File.open(extracted_text_url).read
    # you need to use  Regexp.escape if you have any special character in your from text.

    text = text_main.scan(/#{Regexp.escape(str_from_text)}(.*?)#{str_to_text}/m)
    text = text[0].try(:first).try(:rstrip).try(:to_s)

    text variable will contain the text you want.

    Sunday 29 July 2012

    Show origin and destination on MapQuest with API

    Do you want to show some location on MapQuest or  origin and destination on MapQuest?..then you have come to the right place.
    First, i thought it was a difficult task but after exploring their API's it was a piece of cake :).
    You simply need to create a link to their map search and you are done. Just remember that it doesn't work properly on local server but it work like a charm on Online Servers (So don't panic if it doesn't load on your local server).
    Suppose you want to show the path between  Tyler , TX 75701 and  Fort Lauderdale FL 33311
     your link should look like this:-

     http://mapq.st/directions?saddr=Tyler+TX+75701+(Origin) &daddr= Fort Lauderdale+ FL+33311+(Destination)&vs=directions
    Click here to view on the map.

    Please refer to this link for More details and customization >> Link <<.Please Note that there are some click "Here" links on that page ,which will give you detailed description in tabular form.

    Sunday 1 July 2012

    Welcome to my blog .

    My name is Sachin Prasad and  I'm a software developer at Agami Technologies.I also like  playing games, watching Movies or listening to music.

    I'm on Linkedin, Facebook,Quora and Stackoverflow .
    You can also view my portfolio here.

    I have good deals of experience on (Ruby on rails, PHP and PHP based frameworks).
    Currently working on:
    Ruby on Rails 3.2
    Have also worked on:
    PHP (Codeigniter,CakePHP)

    For any Inquiry or assistance you can contact me on my gmail ac. mastersacdel@gmail.com
     
    I have also moved to brake shoe manufacturing business. To know more about this and visit my website click here.