Thursday, October 15, 2015

solving Ubuntu 14.04 Ruby C dependancy

Given you are running Ubuntu 14.04 and you are installing/upgrading some Ruby gems (let sey Rails 4.2.4 gem, or RSpec, ...) and you get this error:
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/tomi/.rvm/rubies/ruby-2.2.3/bin/ruby -r ./siteconf20151015-21730-1qh2hm1.rb extconf.rb
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling generator.c
linking shared-object json/ext/
/usr/bin/ld: cannot find -lgmp
collect2: error: ld returned 1 exit status
make: *** [] Error 1

make failed, exit code 2

Gem files will remain installed in /home/tomi/.rvm/gems/ruby-2.2.3@maze_magic/gems/json-1.8.3 for inspection.
Results logged to /home/tomi/.rvm/gems/ruby-2.2.3@maze_magic/extensions/x86_64-linux/2.2.0/json-1.8.3/gem_make.out
An error occurred while installing json (1.8.3), and Bundler cannot continue.
Make sure that `gem install json -v '1.8.3'` succeeds before bundling.
sudo apt-get install libgmp-dev
sudo apt-get install libgmp3-dev
should solve the problem.

Monday, August 24, 2015

Rails log - display partials log only

...or: show me only the log of  what is being rendered.
Let say your Rails log is doing to much and you just want to see which partials or layouts are beeing rendered.
cd ~/my-rails-application
tail -f log/development.log                     # entire log as it is

tail -f log/development.log | grep -e 'Render'  # just what is rendered
                                                # partials / layouts
This can be anything you want
tail -f log/development.log | grep -e 'anything'


Wednesday, July 22, 2015

how to include Capybara RSpec matchers in RSpec

let say you want to use have_content and have_selector in non rails-helper spec
# Gemfile
gem 'capybara'
gem 'rspec'
# spec/spec_helper.rb
require 'capybara/rspec'
require 'spec_helper'

RSpec.describe MyPresenter do
  include Capybara::RSpecMatchers

  it do 
    expect("<b>abc</b>").to have_selector("b")

Wednesday, June 17, 2015

Rails mysql gem (>= 2.9.0) throwing errorchecking for mysql_query() in -lmysqlclient... no

My settup:
  • Ubuntu 14.04
  • Rails 4.2.x
  • Ruby 2.2.0


Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/tomi/.rvm/rubies/ruby-2.2.0/bin/ruby -r ./siteconf20150617-4359-1xygr4t.rb extconf.rb 
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** 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.

Provided configuration options:

extconf failed, exit code 1

Gem files will remain installed in /home/tomi/.rvm/gems/ruby-2.2.0@rails-framework-4.2/gems/mysql-2.9.1 for inspection.
Results logged to /home/tomi/.rvm/gems/ruby-2.2.0@rails-framework-4.2/extensions/x86_64-linux/2.2.0/mysql-2.9.1/gem_make.out


sudo apt-get install  libmysqlclient-dev
if not try
 sudo apt-get install mysql-server

Thursday, April 23, 2015

Use Rails (ActiveSupport) delegation class in plain ruby

# Gemfile
source ""
gem 'active_support'
require 'active_support/core_ext/module/delegation'

class Foo
  delegate :call, to: :other

  def other
    ->(){ 'foo' }
# => 'foo'

Tuesday, March 31, 2015

Run multiple instances of RSpec on same machine

Sime times you are stuck with realy badly written Rails project (or other application which comunicates with Database) which entire test suite takes ages to run. You want to run all the tests but you while they running you want to continue your TDD work.
If you run RSpec multiple times (multiple instances at a same time) you may break one of test suits because you will write data to same database both RSpec instances are comunicating with.
One way to handle this is if you run rspecs on different databases
# config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  reconnect: false
  pool: 5
  username: root
  password: my_development_password
  socket: /var/run/mysqld/mysqld.sock

  <<: *default
  database: myapp_development

  <<: *default
  database: myapp_test<%= ENV['TEST_ENV_NUMBER'] %>
 RAILS_ENV=test TEST_ENV_NUMBER=2 rake db:create
 RAILS_ENV=test TEST_ENV_NUMBER=2 rake db:migrate

 TEST_ENV_NUMBER=2 rspec spec/
 If you are looking for solution how to run same test suite on multiple databases check

Override Rails default rake task

desc "this will be now a default task"
task info: :environment do
    puts 'Run rake test to test'


Tuesday, March 3, 2015

Carrierwave uploader not triggering proces in RSpec

processing is turn off for sake of test speed
# spec/my_test.spec
before do
  DocumentUploader.enable_processing = true
It's probably because you have something like
# config/initializers/carrierwave.rb
if Rails.env.test?
  CarrierWave.configure do |config| = :file
    config.enable_processing = false
  CarrierWave.configure do |config| = :fog
I recommend to keep the enable_processing = false and just overide it when needed

Thursday, February 26, 2015

Troubles with reqexp encoding in Ruby

Today friend of my shown me problem that he encounter while he was working on his regexp-examples gem
'<'  =~ /[[:punct:]]/
# => nil

60.chr  # =>  '>'
60.chr  =~ /[[:punct:]]/
# => 0

60.chr == '<'
# => true

60.chr === '<'
# => true
It took him a while to figure out what was the problem
# => #<Encoding:UTF-8>

# => #<Encoding:US-ASCII>
solution :
60.chr.encode('UTF-8') =~ /[[:punct:]]/
# => nil

Wednesday, January 28, 2015

Free up space on your Linux Ubuntu server

You can check your disk space usage with df, or particular folder df -sh.
You can check inode space usage by df -i (if you running caching server with file storage this will be often an issue)

Delete downloaded packages (.deb)

E.g.: already installed (and no longer needed)
sudo apt-get clean

Remove stored archives in your cache

E.g.: packages that can not be downloaded anymore, packages are no longer in the repository or that have a newer version in the repository
sudo apt-get autoclean

Remove packages after uninstalling an application

sudo apt-get autoremove

Remove old unused kernels

list all your kernels (installed and deinstalled) :
dpkg --get-selections | grep linux-image
your currently used kernel
uname -r
to remove particular kernel:
sudo apt-get remove --purge linux-image-X.X.XX-XX-generic
You can also run this script that will remove all unecesarry kernels, Be really carefull with this !!
dpkg -l linux-*  | \
awk '/^ii/{ print $2}' | \
grep -v -e `uname -r | cut -f1,2 -d"-"` | \
grep  -e '[0-9]' | xargs sudo apt-get -y purge
  • dpkg -l linux-* list all kernels
  • uname -r will tell you current kernel

when stuff goes wrong

clean up /boot partition

when you install kernel and you get error similar to this one:
update-initramfs: Generating /boot/initrd.img-3.13.0-62-generic

gzip: stdout: No space left on device
E: mkinitramfs failure cpio 141 gzip 1
update-initramfs: failed for /boot/initrd.img-3.13.0-62-generic with 1.
run-parts: /etc/kernel/postinst.d/initramfs-tools exited with return
code 1
dpkg: error processing package linux-image-extra-3.13.0-62-generic
 subprocess installed post-installation script returned error exit
status 1
No apport report written because MaxReports has already been reached
triggers for initramfs-tools (0.103ubuntu4.2) ...
update-initramfs: Generating /boot/initrd.img-3.13.0-62-generic

gzip: stdout: No space left on device
it may be you run out of space on boot partition
df /boot      # 100%
ls /boot
-rw-r--r--  1 root root  1169023 May 26 20:18 abi-3.13.0-54-generic
-rw-r--r--  1 root root  1169023 Jun 18 01:14 abi-3.13.0-55-generic
-rw-r--r--  1 root root  1169201 Jun 19 10:30 abi-3.13.0-57-generic
-rw-r--r--  1 root root  1169346 Jul  8 04:00 abi-3.13.0-58-generic
-rw-r--r--  1 root root  1169346 Jul 24 23:11 abi-3.13.0-59-generic
-rw-r--r--  1 root root  1169346 Jul 29 12:40 abi-3.13.0-61-generic
-rw-r--r--  1 root root  1169478 Aug 11 15:51 abi-3.13.0-62-generic
-rw-r--r--  1 root root  1169421 Aug 14 22:58 abi-3.13.0-63-generic
-rw-r--r--  1 root root   169832 May 26 20:18 config-3.13.0-54-generic
-rw-r--r--  1 root root   169832 Jun 18 01:14 config-3.13.0-55-generic
-rw-r--r--  1 root root   169832 Jun 19 10:30 config-3.13.0-57-generic
-rw-r--r--  1 root root   169832 Jul  8 04:00 config-3.13.0-58-generic
-rw-r--r--  1 root root   169832 Jul 24 23:11 config-3.13.0-59-generic
-rw-r--r--  1 root root   169833 Jul 29 12:40 config-3.13.0-61-generic
-rw-r--r--  1 root root   169833 Aug 11 15:51 config-3.13.0-62-generic
-rw-r--r--  1 root root   169833 Aug 14 22:58 config-3.13.0-63-generic
Remove the old kernels like this:
sudo dpkg --purge linux-image-3.13.0-53-generic
sudo dpkg --purge linux-image-3.13.0-54-generic
# ...
sudo apt-get -f install # tell to continue installing the latest kernel
sudo apt-get autoremove

source of information