Monday, April 21, 2014

Installing Ruby 2.1.1 in Ubuntu 14.04

The apt-get way don't install the latest stable Ruby (2.1.1), even on Ubuntu 14.04. Instead, I got 1.9.3 that way. RVM can be easy, but to work with Apache I'd need a system-wide installation independent of the RVM environment. So I've decided to install Ruby from source. I had problems around readline, but after seeing this hint, the solution that worked for me is:

./configure --with-readline-dir=/usr/lib/x86_64-linux-gnu/libreadline.so
make
make install

on the directory of the unpacked source of Ruby 2.1.1, as root. I suspect there is a more elegant solution out there, but this is enough for me at this time.

Friday, March 7, 2014

LOAD DATA INFILE tips

If you're having troubles with LOAD DATA INFILE ("Errcode 13"):
  • copy the CSV file to the DB folder (/var/lib/mysql/DATABASE), and
  • use the filename only, not the full path.


If you're having troubles with LOAD DATA LOCAL INFILE ("The used command is not allowed with this MySQL version"):
  • temporary solution: mysql -uUSER -p --local-infile DATABASE
  • permanent solution: sudo nano /etc/mysql/my.cnf and insert local-infile under [mysql]; save the file, close the editor and type sudo service mysql restart.

Note: LOAD DATA LOCAL INFILE uses IGNORE. If you want to know why some records were skipped, you must use LOAD DATA INFILE (without LOCAL). (Reference)

Sunday, September 29, 2013

Best way to install MongoDB in Ubuntu

Refer to http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/.

That's the best way.

If you try to install only thru apt-get, you can get a deprecated version.

If you try by downloading the binaries, it won't install useful things like user and service.

Tuesday, September 24, 2013

How to change the default mode for php files in Kate Editor

Whenever I open a PHP file within the excellent Kate Editor (which I prefer due to its multi-document interface), the default mode for editing the file is PHP (HTML) instead of PHP/PHP. I use much more the indentation of the last one. I always can change the mode thru Tools -> Mode -> Scripts -> PHP/PHP, but it's a pain to do it every time I open a PHP file.

But you can set the default mode to PHP/PHP by changing its priority compared to PHP (HTML)'s.

That's how I did it:

  • Go to Settings -> Configure Kate -> Open/Save -> Modes & Filetypes;
  • Select Scripts/PHP (HTML) Filetype;
  • Change Priority to 4;
  • Select the File Extensions field and copy it;
  • Select Scripts/PHP/PHP Filetype;
  • Go to the File Extensions and paste the content copied;
  • Hit Apply and Ok;
  • Close and reopen Kate.


I was hard to find that solution (see reference); I hope this can be useful.

Wednesday, February 27, 2013

SQLite3 in Ruby 1.9: be sure your Ruby data is UTF-8

SQLite3 is not finding your records? SQLite3 is inserting binary data instead of plain-text strings? If you're using Ruby 1.9 or later, you must be sure your Ruby data is encoded as UTF-8 before submit it to SQLite3.

Text data in SQLite is UTF-8. Before Ruby 1.9, Strings had no encoding; so that was not a issue; all data get same handling. From Ruby 1.9, Strings must be encoded as UTF-8 to be compatible with SQLite data; otherwise your text will be considered binary content to SQLite, and binary content won't match plain-text content, even when they are the very same bytes.

Maybe the source of your text data encodes them as other than UTF-8. To make sure your data is UTF-8, try using String#encode or String#force_encoding before submit it to SQLite 3. Cheers!

Thursday, January 31, 2013

Sequel blocking SQLite3?

I was often getting SQLite::BusyException (reraised as Sequel::DatabaseError) or Sequel::PoolTimeout exceptions when using Sequel to handle SQLite3 databases in a program with little concurrency, in a way that that exceptions are not expected (specially on reads). I rewrote my code to use the SQLite3 gem instead of Sequel, just for test, and I had no more exceptions.

There are things to be investigated, but this tip can be useful to someone as a quick (temporary?) solution.

Friday, January 18, 2013

Some undocumented differences between 1.8 and 1.9

Here I bring some differences among Ruby versions 1.8.7, 1.9.1 and 1.9.2 which I didn't found in other sites, maybe because they represent very rare use cases (or maybe because I didn't search enough). For these cases, version 1.9.3 behaves as 1.9.2. I was faced with them when trying to make Namebox compatible with Ruby 1.8.7 and 1.9.1, but after weeks of work I concluded that it doesn't worth.

Methods names' type


The method names for instance_methods will be String in 1.8.7 and Symbol since 1.9.1.
Since instance_method(method_name)don't care whether method_name is Symbol or String, this seems to be a innocuous difference, but if you code has something like Klass.instance_methods.include?("f") it will break when changing versions. This is also valid for methods and singleton_methods.

class A; def f; end; end

p A.instance_method(:f)         #=> #<Method:A#f>
p A.instance_method('f')        #=> #<Method:A#f>

p A.instance_methods(false)
#=> Ruby 1.8.7: ["f"]
#=> Ruby 1.9.1: [:f]

The superclass of the singleton class of a class


The superclass of the singleton class (also known as eigenclass) of a class X is the singleton class of the superclass of X. This follows the natural way of class methods lookup (not counting the extended modules). However, this works only since 1.9.1. In Ruby 1.8.7, the superclass of the singleton class of any class is the singleton class of the class Class:

class A; end
class B < A; end

# singleton class of B
SB = class << B; self; end

p SB        #=> #<Class:B>

p SB.superclass
#=> Ruby 1.8.7: #<Class:Class>
#=> Ruby 1.9.1: #<Class:A>

Binding class methods to a subclass


For instance methods, you can bind an unbound method to an object since that object is an instance of the method's class or subclass. For class methods, you can bind an unbound method to a subclass of the method's class (or to the class itself). This works since 1.9.2. Earlier versions raises TypeError if you try to bind a class method to a subclass:

class A
  def self.f
    "self is #{self}"
  end
end

class B < A; end

p A.method(:f).unbind.bind(B).call
#=> Ruby until 1.9.1: TypeError
#=> Ruby since 1.9.2: "self is B"

super from a module method after binding


In Ruby 1.8.7, if you bind an instance method of a module to an object (of a class which includes that module), and if that method has super, it will raise NoMethodError instead of looking for the super method. It will flow normally if that method is invoked without bind.

class A
  def f
    "Hello"
  end
end

module M
  def f
    super + " world!"
  end
end

class B < A
  include M
end

b = B.new
p b.f       #=> "Hello world!"

p M.instance_method(:f).bind(b).call
#=> Ruby 1.8.7: NoMethodError
#=> Ruby 1.9.1: "Hello world!"