nunojob:~ dscape/08$ echo The Black Sheep

Posts tagged ‘Rubyonrails’

Information Retrieval using the Boolean Model in Ruby on Rails

famfamfam flag iconset yaml representation on ruby

First download the flags from famfamfam into a folder called flags (pngs only,
no subfolders!).

Now in the bash:

cd flags
wget http://www.iso.org/iso/iso3166_en_code_lists.txt
ls > file.txt
irb

Now your on the interactive ruby shell (irb).

# If you want to use this as a script just copy 
# the bash lines and put them as
# system 'cd flags'
# system 'wget ...' and so on

# We start treating the output from the ls
# open the ls output
f = File.new 'file.txt'                            
# place the file in lines
lines = f.readlines                                
# map those who have 2 digits codes
lines = lines.select { |line| line.size == 7 }    
# get the 2 digits 
lines = lines.map { |line| line[0..1] }            
f.close

# Then the iso file
# open the iso file
f = File.new 'iso3166_en_code_lists.txt' 
# get rid of the notes          
f.readline                                         
# place the file in iso
iso = f.readlines                                  
# create a new hash
hashed_iso = {}                                    
# select non empty lines
iso.select { |a| !a.rstrip.empty? }.map do |b|
  # remove the whitespaces and split in ';'     
  aux = b.rstrip.split ';'     
  # place info in the hash                    
  hashed_iso[aux[1].downcase] = aux[0].capitalize  
end
f.close

# Now we cross information giving more 
# importance to what's in the iso.
iso_famfamfam = hashed_iso.select {
  # select those who have flags in famfamfam
  |k,v| lines.member? k                            
}.sort_by {
  # sort them by the name the user will see
  |pair| pair[1]                                   
}

# Now we create the contents to store in the yaml file
# create the yaml first line
yaml_lines = "hash: \n"                           
iso_famfamfam.each do |pair|
  # for each pair, create the yaml
  # representation and put in yaml_lines
  yaml_lines < < '  ' + pair[0] + ': ' 
                         + pair[1] + "\n"    
end 

yaml_lines &lt;&lt; "array: ---\n"

iso_famfamfam.each do |pair|
  yaml_lines << '- - ' + pair[0] + "\n  - " 
                            + pair[1] + "\n" 
end 

# put it in a file
f = File.new 'flags.yml', 'w'
f.write yaml_lines
f.close

#sample for loading the yaml into ruby
f = File.new 'flags.yml'
fy = YAML.load f

Now you have your yaml representation of the flags. Do what you please. Personally I’m going to use the file to load it to Ruby when my Ruby on Rails app starts and use it as part of the registration system in the

DB2 Rocks

qs = Question.find_by_sql 
"select X.* from ots_schema.questions," +
  "XMLTABLE (\'$d/question\' passing document as \"d\" " + 
    "COLUMNS question_text VARCHAR(200)" + 
    "PATH \'question_text\') as X"
qs.first.question_text.lstrip
=> "Which of the following is the correct syntax to set the DB2COMM variable to TCPIP?\n  "

If DB2 was had a good DB2 driver and a ActivePureXML (or something adapter) it would so f*ckin rock. Just look at the sample. And the dynamic nature of ruby would enable the flexibility of xml documents.

Please IBM please. DB2 for mac and decent support on Ruby. Don’t make me write things like this no more:

# Once again fixing IBM_DB bugs the ugly way
# with_scope anyone?
add_index :'ots_schema.users', :login

or

t.column :document, :xml

Google reinvents the wheel

About porting Ruby on Rails to Javascript:

In an effort to increase developer productivity at Google, Steve tried to convince the company to adopt Rails (and consequently Ruby) as a programming language. When that fell on deaf ears (Google really does not want to increase the number of languages that must be supported by their infrastructure), Steve decided to do what any other frustrated programmer would do: he ported Rails to JavaScript. Line by line. In 6 months. Working 2000 hours. Steve is a coding stud.

From: Steve Yegge ported Rails to JavaScript

In this six months Steve could have contributed to the rails core and improved the framework to a great extent. If he found security issues like the article refers than he should have fixed them in rails. I cannot even begin to understand why he didn’t by the way. Or at least reported them.

My conclusion of seeing that google allowed a employee to waste 6 months of work because they don’t want to increase the number of languages that must be supported by their infrastructure is that Google is Dumb.

Steve seems like a smart guy so if I was told to rewrite Ruby on Rails by an employer I cannot understand why he didn’t – at the very least – refused to do so and pointed out how stupid it was to reinvent the wheel. I simply hate javascript and love ruby. I think javascript is the worst thing there is in the internet. That’s one of the reasons I decided to take a chance on XForms. Well but that’s a personal opinion and has nothing to do with the case!
The question is:

So will this bring something new to the web?

Yes! But it’s not that this wheel is great and the other was flat. It can bring something but just because Google has the power to do that, in any language they decide to use. They have the man power to go beyond what rails offer.

But does this make the decision less dumb?

No. They could do it in rails and improve a great open source product.
Or is it that hard for engineers at google to learn a new language??

has_one :through

Finally we will be able to refactor our ugly code when we have this situation (changeset 9067):

class User < ActiveRecord::Base
  has_many :channels
end
class Channel < ActiveRecord::Base
  belongs_to :network
  belongs_to :user
end
class Group < ActiveRecord::Base
  has_many :channels
  has_one :network, 
    :through => :channels,
    :conditions => ['channels.alive? = ?', true]
end

Another good news is that the RubyForge place for the OTS project was finally approved. So guess I will be doing some work on it this afternoon.

Valid and Well-formed XML Documents in Ruby on Rails

If your working with DB2 on Rails you probably need to check if the xml document is – at least – well formed.

I found a neat plugin called validates-xml that uses REXML to see if documents are well formed.

To install it simply

svn co http://validates-xml.googlecode.com/svn/trunk/ validates-xml-read-only

And copy that folder to vendor/plugins.

You can easily integrate schema validation with a validates_xml_with_schema method. But you should use REXML as it cames with the standard ruby bundle since version 1.8.

To use it simply restart your server and

validates_xml :xmldocument

That’s it!

Yet Another Ruby on Rails Web Application – Teaser

Working different models in the same form on Ruby on Rails

Well it’s been a heck of a week, loving somethings on rails and hating others :P

Anyway I had a problem with working with different models on a form. If you are having the same kind of issues I’ll give you some pointers:

My notes file is so big I think I’ll never solve all those issues. *g* Anyway another pointer I can give you is to check out haml. I didn’t used it but it sure is simpler than html.

Random in Ruby on Rails

So your doing a find on your model and you want a random instance. I didn’t find a method to do this so I decided to make my own.

In this case I wanted a random quote. So after generating the model all I had to do was:

def self.random
  Quote.find_by_id rand(Quote.count) + 1
end

And now you can simply call your random quotes in any controller action you desire. Just

@rquote = Quote.random

If this is the lamest ruby ever please feel free to give me some feedback :) I will be delighted to learn a different way!

[EDIT: Discussion about this at Rails Core]

This version of RMagick was created to run with ImageMagick 6.3.3 but ImageMagick 6.3.8 is in use

A paciencia tem os seus limites e vou dormir. Se entretanto alguém descobrir a solução para isto peço-lhe o favor de me deixar nos comentários.

Life on the rails

I found Ruby in a talk with one of my friends. We were talking about how a problem that can be solved in Haskell in one line of code, could take hundreds to do so in Java or C#. It was obvious that, for some specific problems, Haskell was simpler and adequate.

Want a sample? Code permutation with n levels in C.

perms xs 0 = [[]]
perms xs n = [ p : ps | p <- xs, ps <- perms xs (n - 1)]

And like this he introduced me to Ruby on Rails. I was very thrilled to see a language that is dynamic, object oriented and adequate for web developing. I immediately thought about learning it, I just love learning new languages.

Today I checking my feeds and found a post about seven reasons to switch back to PHP (from Ruby).

It seems Ruby is like Haskell: the code looks greats and promises a lot, but it still doesn’t get where imperative languages do. (I’m still going to learn it, I just can’t resist it)

For now I’m waiting for a language that joins all that is good from imperative, functional, logic and object oriented paradigms.

If you know such programming language leave it as a comment.

 

 

PHP $61,000

 

Ajax $76,000

 

Ruby $70,000

 

Java $77,000

 

C# $78,000

 

Haskell $52,000

 

View Larger Salary Graph

Follow

Get every new post delivered to your Inbox.