Coding is an Art

Anoiaque::Ergasterium

03/09/2010

Why :_:: == :_ is true in Ruby

The origin of this post is a tweet of PragDav Retweetted by Igrigorik (http://twitter.com/igrigorik/status/22767684142)
I've digged a bit to answer why :_:: == :_ is evaluated to true.

First, you can access to instance or class methods in ruby like this : Foo.new::say_hello => "Hello"

:_ is a symbol so an instance of Symbol class

So
:_ has methods(you can list it by :_::methods)

Therefore
:_:: == is the call to the method == of the instance :_ of the Symbol class

Therefore
:_::==:_ can be interpreted as == (:_, :_) where == is the method

As == (:_, :_) is true , :_::==:_ is true



QED

18/07/2010

How to get and send image with Rails without paperclip or other image gem

How to get an image or binary data from an input file from client side, without paperclip or other image gem.
class Picture < ActiveRecord::Base 
  
  def image_file=(uploaded_data)
    self.content_type = uploaded_data.content_type
    self.filename = uploaded_data.original_filename
    self.image_data = uploaded_data.read
    self.size  = uploaded_data.size 
  end
  
end

In controller, to get the file data and to send it to client :

class PicturesController < ApplicationController
  
  def update
    @picture.image_file = params[:picture] #FileTemp object
    @picture.save
  end
  
  def code_image
    @image_data = Picture.find(params[:id])           
    @image = @image_data.binary_data           
    send_data (@image, :type => @image_data.content_type, :filename => @image_data.filename, :disposition => 'inline') 
  end

end

String concatenation benchmark in ruby

What to use for concatenate strings in ruby , "+=" or "<<" or concat() ? Here is a benchmark that can guide if concatenations is in a loop.
def benchmark_string_concatenation n
Benchmark.bm(10) do |x|
x.report("With '+=' :") {string = ""; n.times {|n| string += "e"} }
x.report("With '<<' :") {string = ""; n.times { |n|string << "e"} }
x.report("With #concat :") {string = ""; n.times { |n|string.concat("e")} }
end
end


Results with n = 5000:
              user      system    total     real
With '+='   : 0.010000 0.010000 0.020000 ( 0.022831)
With '<<'   : 0.000000 0.000000 0.000000 ( 0.001734) 
With concat : 0.000000 0.000000 0.000000 (0.001689)


Results with n = 50000:
                user    system    total   real
With '+='   : 0.710000 0.000000 0.710000 ( 0.737932)
With '<<'   : 0.020000 0.000000 0.020000 ( 0.019771)
With concat : 0.020000 0.000000 0.020000 ( 0.019853)

Recursively chained send on an object with Array#inject

Suppose you have an arbitrery and dynamic list of methods to call on an object.
For example you have Cat class with methods :
#drink_milk
#sleep
#climb_curtain
#purr
And you want a method taking an arbitrery list of actions to execute as parameter.
With powerful inject method ... so easy to do :

class Cat

  def drink_milk
    puts "I drink milk"
    self
  end

  def sleep
    puts "I sleep"
    self  
  end

  def climb_curtain
    puts "I climb the curtain"
    self  
  end

  def purr
    puts "Miaouu"
    self  
  end


  def do_these_things actions
    actions.inject(self) {|cat, action| cat.send(action)}
  end

end

cat = Cat.new
cat.do_these_things [:sleep, :drink_milk] # => I sleep I drink milk
cat.do_these_things [:purr, :drink_milk, :climb_curtain] # => Miaouu I drink milk I climb the curtain

02/05/2010

Exporter une collection d'objets ruby vers un fichier excel

Permet d'exporter des collections de différentes classes.
Permet d'inclure les attributs d'associations.

Installation :
gem install to_excel
Source + documentation : to_excel github repository
Gemcutter : to_excel gem

28/04/2010

Nombres premiers : Classe Prime de la librairie 'mathn' de ruby

>> require 'mathn'
=> true
>> primes = Prime.new
=> #<Prime:0x1014752a0 @counts=[], @primes=[], @seed=1>

# les n premiers nombres premiers
>> 3.times { puts primes.succ }
2
3
5

# décomposition en facteurs premiers
>> 123.prime_division
=> [[3, 1], [41, 1]]
>> 17.prime_division
=> [[17, 1]]
> 13333339.prime_division
=> [[13333339, 1]] # hé oui :)

# PGDC de deux entiers
>> 12.gcd2(234)
=> 6

# La réciproque de prime_division
>> 13344443.prime_division
=> [[7, 1], [59, 1], [79, 1], [409, 1]]
>> Integer.from_prime_division(13344443.prime_division)
=> 13344443
>> primes.last  # :)

Un peu de mathématiques avec la librairie 'mathn' de ruby

# Sans mathn

require 'complex'
require 'matrix'
36/16 => 1.414... 
Complex::I ** 2 => Complex(-1, 0)
m = Matrix[[1,2],[3,4]]
i = m.inv

m*i => Matrix[[1,0],[-2,-2]] #:(

#avec mathn

require 'mathn'

36/16 => 9/4... 
Complex::I ** 2 => -1
m = Matrix[[1,2],[3,4]]
i = m.inv
m*i => Matrix[[1,0],[0,1]]

Fools ignore complexity. Pragmatists suffer it. Some can avoid it. Geniuses remove it.
Alan Perlis


The best way to predict the future is to invent it.
Alan Kay


Ne découvre de nouvelles terres que celui qui sait quitter tout rivage.
André gide


Qui ne doute pas acquiert peu
Léonard de Vinci


Our life is frittered away by detail... simplify, simplify.
Henry David Thoreau