Ramblings Of a Madman

Yeah, like i would know a good description…

Posts for Tag: ruby

Problem starting Sidekiq in development

If you ever get this error:

can't link outside actor context

Followed by something like:

    /Library/Ruby/Gems/2.0.0/gems/celluloid-0.16.0/lib/celluloid.rb:176:in `new_link'
    /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/lib/sidekiq/launcher.rb:21:in `initialize'
    /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/lib/sidekiq/cli.rb:81:in `new'
    /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/lib/sidekiq/cli.rb:81:in `run'
    /Library/Ruby/Gems/2.0.0/gems/sidekiq-3.3.4/bin/sidekiq:8:in `<top (required)>'
    /Library/Ruby/Gems/2.0.0/bin/sidekiq:23:in `load'
    /Library/Ruby/Gems/2.0.0/bin/sidekiq:23:in `<main>'

It's mostly likely somehow related with either ZenTest or another testing framework. 

In my case, i was adding the ZenTest gem both in the development and test groups of my Gemfile. Moving it away from the development group solved the problem.

'Error compiling pg Ruby gem on OSX'

If you get this error (or similar) while trying to run **bundle install** on one of your Ruby projects or trying to install the **pg** gem:

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/Users/void/.rvm/rubies/ruby-2.0.0-p0/bin/ruby extconf.rb --with-iconv-include=/usr/local/opt/libiconv/include --with-iconv-lib=/usr/local/opt/libiconv/lib checking for pg_config... no No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.

The solution is to install the gem like this:

sudo env ARCHFLAGS="-arch i386" gem install pg -- --with-pg-include=/Library/PostgreSQL/8.4/include/ --with-pg-lib=/Library/PostgreSQL/8.4/lib/

**Note:** Replace 8.4 with your installed PostgreSQL version in the above command.

Ruby bundle dependency tree

On OSX:

brew install graphviz && gem install ruby-graphviz && bundle vi

On Linux:

apt-get install graphviz && gem install ruby-graphviz && bundle viz

On Windows:

Good luck! ;)

This will generate a png image with all the dependencies. Have fun!

Date and time in ruby

Time.now will give you the current date (and time), like so:

Time.now 2012-11-22 22:35:01 +0000

You can also request all the date parts individually:

Time.now.hour 22 Time.now.min 35 Time.now.sec 01 Time.now.day 22 Time.now.month 11 Time.now.year 2012

Cool (and easy), right? If you want to change a date, you can add seconds to it:

t = Time.now 2012-11-22 22:35:01 +0000 t2 = t + 10 # 10 Seconds 2012-11-22 22:35:11 +0000 t3 = t + 10*60 # 10 minutes 2012-11-22 22:45:01 +0000 t4 = t + 10*60*60 # 10 hours 2012-11-23 08:35:01 +0000

If you happen to be developing a Ruby on Rails application then it gets even easier:

t = Time.now 2012-11-22 22:35:01 +0000 t2 = t + 1.hour 2012-11-22 23:35:01 +0000

That&#39;s right, in Rails numerals get some extra methods injected into them, like hour, hours, minute, minutes, second, seconds, day, days, and so on... And people still ask me why i love Ruby (and Rails)... :)

A collection of useful Ruby/Rails gems

Since lately i&#39;ve been working a lot with Ruby (Android and Java&nbsp;as well, but that&#39;s a different story), i figured i posted a couple of interesting gems that will make your life easier, especially but not only for rails.

Slim

Slim is a templating engine (like ERB or HAML) which pursues simplicity. It helps you write an insane amount less HTML and Rails code. You will not believe your eyes. An example:

In HTML

&lt;body&gt; &lt;h1&gt;Markup examples&lt;/h1&gt; &lt;div id=&quot;content&quot;&gt; &lt;p&gt;This example shows you how a basic Slim file looks like.&lt;/p&gt; &lt;%= yield %&gt; &lt;/div&gt; &lt;/body&gt;

The same in Slim

body &nbsp; h1 Markup examples &nbsp; #content &nbsp; &nbsp; p This example shows you how a basic Slim file looks like. &nbsp; &nbsp; == yield

Now tell me which one you prefer to write ;)

Stamp

A fantastic tool to format dates based on human readable formats. Why use Date.today.strftime(&quot;%Y/%m/%d&quot;) when you can use Date.today.stamp(&quot;2012/12/13&quot;)? Sweet, right?

Bugsnag

Bugsnag is web site exception grabbing on steroids. It basically monitors your website alerting you of any exceptions and providing the relevant information needed so you don&#39;t have to debug your production server.

Cool thing is, there are &quot;bindings&quot; for multiple languages and frameworks. You can find the one for Ruby/Rails here.&nbsp;

Honey Badger

Honey badger has a really simple purpose: To make sure you know which server you are in (say, testing, dev, production) by displaying a small &quot;badge&quot; with the name of your choosing.

If you are like me you can easily think you are debugging something in testing when you are really looking at production. Ok, maybe that&#39;s just me, but the badge does look cool, right? :)

God

Now, i&#39;m not one to call the name of the Lord in vain (i am!), but God is something i can no longer do without. It&#39;s a process monitoring framework written in ruby that keeps an eye on your server&#39;s, say nginx, apache or mysql and makes sure they are always up.

Further checks can be done, like for example restart the server when memory usage goes above X or CPU usage goes above Y. It is also capable of notifying you by email, jabber, twitter, you name it, when things happen. It&#39;s a true life saver :)

Hope you find these gems useful, as i did, and if you have some that i might find interesting, please do share via twitter :)

Count (group by) number of line occurrences in file

Here's a neat ruby script to group and count the number of occurrences of lines inside a given file.

count.rb list = IO.readlines(ARGV[0]) h = Hash.new {|hash, key| hash[key] = 0} list.each {|item| h[item] += 1} h = h.sort_by { |k,v| v }.reverse h.each_with_index do |p,i| puts "(#{p[1]}) #{p[0]}" if ARGV[1] and ARGV[1].to_i &lt;= i + 1 break end end Usage is: ruby count.rb &lt;file&gt; [show_at_most_n] If you have a file named "faren.txt" comprised of the following lines: test test awesome dude dude dude faren meran Running the script  with: ruby count.rb faren.txt Will yield the following result: (3) dude (2) test (1) awesome (1) faren (1) meran In addition, if you pass the "show_at_most_n" parameter (a number), it will only print that number of results. For example 2 will show the following: (3) dude (2) test