» Publishers, Monetize your RSS feeds with FeedShow: More infos (Show/Hide Ads)
Meanwhile, the Microsoft Silverlight 1.1 CTP was disclosed, said to support JavaScript, C#, VB, Ruby, and Python.The way the ruby support came about apparently was this:
Once Iron Python was created, Microsoft teams looked to see what other languages could be supported, and what aspects of the Iron Python experience “could be factored out into a set of libraries that could be useful to other people trying to build other dynamic languages,” said Lam, now program manager on the Common Language Runtime team at Microsoft.So what this actually means as far as the potential could be great but that will be later down the line. The important thing here is that Microsoft is making steps towards supporting the ruby programming language. Ruby is getting some well deserved attention and this will surely aid in continuing its growth and development for years to come.
Meanwhile, the Microsoft Silverlight 1.1 CTP was disclosed, said to support JavaScript, C#, VB, Ruby, and Python.The way the ruby support came about apparently was this:
Once Iron Python was created, Microsoft teams looked to see what other languages could be supported, and what aspects of the Iron Python experience “could be factored out into a set of libraries that could be useful to other people trying to build other dynamic languages,” said Lam, now program manager on the Common Language Runtime team at Microsoft.So what this actually means as far as the potential could be great but that will be later down the line. The important thing here is that Microsoft is making steps towards supporting the ruby programming language. Ruby is getting some well deserved attention and this will surely aid in continuing its growth and development for years to come.
This is offtopic but I have been watching some great movies lately and wanted to share some of my absolute favorite titles with all of you readers. This may not help your ruby on rails coding but some of these may definetely alter the way you think and perceive the world. If nothing else they can be enjoyed and thought about. I've included several foreign films as well so if you've never seen a foreign film check out some of the ones listed below. A personal favorite is Cidade de Deus. Share any in the comments that you might add to this list.
More Recently Added:
American History X
American Pie
American Psycho
Boondock Saints
Coach Carter
Good Will Hunting
Layer Cake
Saving Private Ryan
Scarface
Terminator
The Matrix
The Usual Suspects
V for Vendetta
Recently Added:
Accattone
Dangerous Liaisons
Goodfellas
Head, The Monkees
LA Confidential
Kelly's Heroes
Night Of The Hunter
Once Upon A Time In America
One Flew Over The Cuckoos Nest
Raging Bull
The Last Picture Show
The Pursuit of Happyness
Vanilla Sky
Original List:
12 Angry Men
8 1/2 - Federico Fellini
A Clockwork Orange
Akira Kurosawa - Seven Samurai
Alfie
Amores Perros
A Streetcar Named Desire
Awakenings
Baraka
Blues Brothers
Blue Velvet
Carandiru
Cidade de Deus (City of God)
Cinema Paradiso
Cool Hand Luke
Dark City
Doctor Zhivago
Dogma
Donnie Darko
Dreams (Akira Kurosawa)
Dr. Strangelove
Earth (Deepa Metha)
Eternal Sunshine of The Spotless Mind
Fahrenheit 451
Fear and Loathing in Las Vegas
Forrest Gump
Furyo Anego Den
Heart of Darkness
Ichi The Killer (Artsy Japanese Film)
Irreversible
JAWS
La Dolce Vita (Federico Fellini)
La Meglio Gioventu (Italian)
Leaving Las Vegas
Let Him Have It
Los Gritos Del Silencio (The Killing Fields)
Machuca
Memphis Belle
Mindwalk
Monster (2003)
Naked Lunch
Needful Things
On the Waterfront
Out of Sight
Philadelphia
Requiem For a Dream
Roger Dodger
Samotari
Serenity
Shane
Shawshank Redemption
Silence of the Lambs
Sunset Blvd (1950)
Swimming with Sharks
The Basketball Diaries
The Bicycle Thief
The Boondock Saints
The City of Lost Children
The Day the Earth Stood Still
The Deer Hunter
The Departed
The Devil's Advocate
The Devil's Rejects
The Fountain
The Great Escape
The Great Santini
THX 1138
Tre Colori (Kieslowski)
Tsotsi
Videodrome
Visitor Q
What Dreams May Come
Whipped
Wings of Desire
There are numerous advantages to learning about the scope plugins available today for ruby on rails development. They can make your code DRYer and more readable and actually save you quite a bit af actual code. As you'll see when visiting the links I am providing with this post, the plugin scope_out for example can provide you with additional methods.
His sites example shows doing:
class Person < ActiveRecord::Base scope_out :women, :conditions => ['people.sex = ?', 'F'] end
The above code creates three class methods: findwomen, withwomen, and calculate_women. It is equivalent to doing the following:
class Person < ActiveRecord::Base def Person.with_women with_scope :find => {:conditions => ['people.sex => ?', 'F']} do yield end end def Person.find_women(*args) with_women {find(*args)} end def Person.calculate_women(*args) with_women {calculate(*args)} end end
There is more to scopeing issues in ruby on rails then what is presented in scope_out.
If your with_scope cases are in controllers as opposed to models, you can do for example:
class Movie < ActiveRecord::Base def self.with_playing with_scope :find => { :conditions => [ ‘state = ? AND visible = ?’, NOW_PLAYING, true ] } do yield end end end class MovieController < ApplicationController def director Movie.with_playing do @director = Movie.find(params[:movie_id]).director end end end
The following sites will open up a wide range of possibilites for you for best use cases for scope in ruby on rails:
The Scope Out Code and Project scope_out
Hobo creator insists you check out scope out... Here's why
Scoped Proxy Plugin for Ruby on Rails
Association Scope Plugin on Ruby on Rails Wiki
How to scope your controller without around_ filter (scope_out will allow you to do what this post explains in detail)
Here's an example using scoped_proxy.
class User < ActiveRecord::Base scoped_proxy :admins, :find => { :conditions => ['role = ?', 'super_user'] } scoped_proxy :has_login do |login| { :find => { :conditions => ['login = ?', login] } } end scoped_proxy :no_op do nil end end # This gives you the first administrator of the system User.admins.find(:first) # This counts the administrators User.admins.count # All users with a given login User.has_login('foo').count User.has_login('foo').find(:all, :order => 'created_at desc') # And finally, I give you the no op User.no_op.find(:all) # => User.find(:all)
Remember that blog named Typo, the one that was really active under development and a lot of us got our start with when rails first became known as the hip framework that it is? Well, they’ve been plugging away for six months or so in the back of seedy bars and diners on their laptops to finally release their 4.1 version with talk of 4.2 soonafter. I even have to upgrade myself for the ruby on rails blog which is still running typo for those of you curios enough to ask…
Here’s the scoop on what’s new:
The changelog is quite impressive, but I’ll only deal with the visible part of the iceberg :
* Ruby on Rails 1.2 support.
* Complete functionnal revamping of the back office, and partial ergonomic rebuild.
* Internationalization and localization support using localization plugin. The application now runs in French.
* Comment and trackback default moderation.
* Lots of bugfixes and code improvement.
* RSS support for tags and categories.
* Plugins now use Rails plugin engine. We’re gonna release packed plugins soon.
Typo 4.2 is due in 2 months, and the roadmap is quite impressive :
* Support of a publishing workflow and users roles.
* Multiple blogs support with a single Typo instance.
* Switch from Localization to Globalization.
* Integrate proposed patchs as plugins.
* Finish the admin revamping.
* Support more languages.
* Stop doing stupid things like starting to support localization the day before the planned release date.
The project is looking for translators a designer to work with me on the admin while I’m doing the ergonomic stuffs.
You can download the source or install Typo via the gem :
laptop # gem install -y typo laptop # typo install /some/path
[edit] There’s a bug in the migration process if you come from the 4.0 version. Before doing the migration, edit db/migrate/056createnotifications.rb and comment the following line : drop_table :notifications
If you have already started the migration, comment both lines : renametable :notifications, :oldnotifications drop_table :notifications
The Ruby on Rails team is at it again with another update to… you guessed it, Ruby on Rails. According to the administration, this release irons out the few wrinkles there was between Ruby 1.8.6 and Rails 1.2.2. Not a critical upgrade for most but a useful one for all of us using Ruby 1.8.6.
On another note, why stop at upgrading rails.
Check out the latest version of mongrel as well. That fine piece of software has really gone a distance. That isn’t to say I haven’t had some unexplainable issues lately with session freezes but that could be config issues on my part. I’ll actually be posting about it once Tommy or I figure it out.
To upgrade both of them, simply do: gem update rails mongrel -y
And there you have it! Have fun and try not to lose your wrench!
irb> $".inject([]){|s,v| s + [File.join($:.find{|pa| File.file?(File.join(pa, v))}, v)]} => ["/usr/lib/ruby/1.8/irb.rb", "/usr/lib/ruby/1.8/e2mmap.rb", "/usr/lib/ruby/1.8/irb/init.rb", "/usr/lib/ruby/1.8/irb/context.rb", "/usr/lib/ruby/1.8/irb/workspace.rb", "/usr/lib/ruby/1.8/irb/extend-command.rb", "/usr/lib/ruby/1.8/irb/ruby-lex.rb", "/usr/lib/ruby/1.8/irb/slex.rb", "/usr/lib/ruby/1.8/irb/notifier.rb", "/usr/lib/ruby/1.8/irb/output-method.rb", "/usr/lib/ruby/1.8/irb/ruby-token.rb", "/usr/lib/ruby/1.8/irb/input-method.rb", "/usr/lib/ruby/1.8/i486-linux/readline.so", "/usr/lib/ruby/1.8/irb/locale.rb", "/usr/lib/ruby/1.8/irb/completion.rb", "/usr/lib/ruby/1.8/irb/ext/history.rb", "/usr/lib/ruby/1.8/irb/ext/save-history.rb", "/usr/lib/ruby/1.8/benchmark.rb"]
gem install rails --include-dependencies ...or if you prefer to freeze it: rake rails:freeze:edge TAG=rel_1-2-1
gem install rails --source http://gems.rubyonrails.org -yIn an application where a user is given the ability to upload or edit a css file, you may want to allow them to validate it against the better known css validatation standards. This ruby script can do just that, although it should be mentioned that it depends on curl and thus is only going to run under windows if you use something akin to CYGwin.
Here is the ruby script.
#!/usr/bin/env ruby curl = `which curl 2>/dev/null`.chomp validator = 'http://jigsaw.w3.org/css-validator/validator' # All warnings, CSS2, all mediums options = 'warning=2&profile=css2&usermedium=all' base = File.expand_path("#{File.dirname(__FILE__)}/../../public/stylesheets") # Got curl? raise "Curl not found" if curl.empty? # Get path to stylesheets if ARGV.size > 0 base = ARGV.shift end # All css files or just one? glob = base =~ /css$/ ? base : "#{base}/*.css" # Do files Dir.glob(glob) do |file| next unless File.exists?( file ) errors, warnings = [ ], [ ] # Send the css file to the validator results = `#{curl} -s -F "file=@#{file}" -F "#{options}" #{validator}` # Validator couldn't find the file # # OR the file didn't have _any_ valid css content before # the errors <- little gotcha # results.grep(/No style sheet found/) do |line| STDERR << "#{$&}\n" exit end # Add new lines to <li></li> tags so grep can find them easier results.gsub!(/\n/,'').gsub!(/<li>/,"\n<li>").gsub!(/<\/li>/,"</li>\n") results.grep(/<li>.*<\/li>/) do |line| # collect errors line.grep(/<span class='error'>/) do |error| errors << error.gsub!(/(<p>|<\/p>)/,"\n").gsub!(/<(.|\n)*?>/, '') end end # collect warnings results.grep(/<span class='warning'>/) do |line| warnings << line.gsub!(/<(.|\n)*?>/, '') end # Dump information to STDERR STDERR << "CSS File #{file}:\n\n" { 'Errors' => errors, 'Warnings' => warnings }.each do |k,v| if v.empty? STDERR << "No #{k.downcase} found\n---------------\n\n" else STDERR << "#{k} found:\n-------------\n\n" v.each {|line| STDERR << line} end end end
Rubyzip
Rubyzip is a gem you can use to zip and unzip files and directories. Here is a very simple piece of code for zipping multiple directories into a single zip file.Zip::ZipFile::open("filename.zip", true) {|zf| Dir['{dir1,dir2}/**/*'].each { |f| zf.add(f, f) }}
Unzipping it is done like this:
unzip_dir="out" Zip::ZipFile::open("filename.zip") { |zf| zf.each { |e| fpath = File.join(unzip_dir, e.name) FileUtils.mkdir_p(File.dirname(fpath)) zf.extract(e, fpath) } }
The Goal
Take this array: [1,3,5,5,6,7,9,10,14,18,22,22,4,4,4,3,6]
Turn it into this array: [5, 22, 6, 3, 4]
Before I go on further, credit is given where credit is due. The following tests, trials and solutions are the product of myself and suggestions from some of my friends from the ruby community: Piers Cawley, Simon Schubert and Rein Henrichs.
SPOILER
The solution and helper to this is at the very end of the post.First Attempt
The first attempt was the most obvios but incorrect.array - array.uniq
Second attempt using XOR
require 'set' => true array = [1,3,5,5,6,7,9,10,14,18,22,22,4,4,4,3,6] => [1, 3, 5, 5, 6, 7, 9, 10, 14, 18, 22, 22, 4, 4, 4, 3, 6] b = (Set.new(array) ^ array).to_a => [5, 22, 6, 3] b => [5, 22, 6, 3] # 4 SHOULD be in the above array 'b' array = [1,3,5,5,6,7,9,10,14,18,22,22,4,4,3,6] => [1, 3, 5, 5, 6, 7, 9, 10, 14, 18, 22, 22, 4, 4, 3, 6] b = (Set.new(array.sort) - array.sort).to_a => [] b = (Set.new(array) ^ array).to_a => [5, 22, 6, 3, 4] # 4 IS IN THE ABOVE BECAUSE THERE ARE ONLY TWO 4s in the orginal.
Third Attempt Using If Then Else
# SAME THING AS ary.uniq array = [1,3,5,5,6,7,9,10,14,18,22,22,4,4,4,3,6] h = {}; array.map{|i| unless h[i] then h[i] = true; i; else; nil; end}.compact => [1, 3, 5, 6, 7, 9, 10, 14, 18, 22, 4]
Fourth Attempt
## Deletes the first instance of each one that occurs more than once # ARRAY, HASH, EACH, UNLESS, LOCAL VARIABLES array = [1,3,5,5,6,7,9,10,14,18,22,22,4,4,4,3,6] b=[]; h = {}; array.each{|i| unless h[i] then h[i] = true else b << i end}; b => [5, 22, 4, 4, 3, 6] # ADD ON .uniq! to get rid of the extras b=[]; h = {}; array.each{|i| unless h[i] then h[i] = true else b << i end}; b.uniq! => [5, 22, 4, 3, 6]
The Working Solutions
A few ways to solve this problem were finally discovered, some prettier than others. The next three examples each solve this problem slightly differently, the code is somewhat self-explanatory.## INJECT AND HASH array = [1,3,5,5,6,7,9,10,14,18,22,22,4,4,4,3,6] array.inject(Hash.new(0)) {|h,v| h[v] += 1; h}.reject {|k, v| v == 1}.keys => [5, 22, 6, 3, 4] # ARRAY, HASH, EACH, DELETE_IF array = [1,3,5,5,6,7,9,10,14,18,22,22,4,4,4,3,6] h=Hash.new{|h,k| h[k]=0}; array.each{|k| h[k]+=1}; h.delete_if{|k,v| v <= 1}; array=h.keys => [5, 22, 6, 3, 4] # ^ IMPROVED to get rid of the Hash.new block by using 0 as a value object h=Hash.new(0); array.each{|k| h[k]+=1}; h.delete_if{|k,v| v <= 1}; array=h.keys => [5, 22, 6, 3, 4]
The Rails Dups and Dups? Helper Methods
And here is what you have all been waiting for, the helper put together by Rein Henrichs. Yes, his last name is plural. =)#### .dups and .dups?: class Array # count returns a hash of item, count pairs def count counts = Hash.new(0) self.each { |item| counts[item] += 1 } end def dups self.count.dups end def dups? self.count.dups.empty? end end class Hash def dups self.reject{|h,v| v == 1}.keys end end
Update - 07/07/2007 (by tshine)
This puzzle has been revisited by some verteran Ruby coders in IRC (carter, njero, divoxx, lisa). divoxx and njero came up with some compelling solutions even if they cheat a bit (see if you can spot where) Source code follows:require 'benchmark' n = 10000 array = Array.new(n) {rand(n/2)} #array = [1,3,5,5,6,7,9,10,14,18,22,22,4,4,4,3,6] #puts "number of dups: " + array.select { |i| array.grep(i).size > 1 }.uniq.size Benchmark.bm do |x| x.report("hash ") { h=Hash.new(0); array.each{|k| h[k]+=1}; h.delete_if{|k,v| v <= 1}; new_array=h.keys; } x.report("divoxx ") { count = Hash.new(0); array.select { |i| count[i] += 1; count[i] == 2 }} x.report("divoxx2 ") { counts = Array.new(n); array.select { |i| counts[i] ||= 0; counts[i] += 1; counts[i] == 2 }} x.report("divnjero") do hcount = Hash.new res = Hash.new array.each do |obj| hcount.key?(obj) ? res[obj] = nil : hcount[obj] = nil end res.keys end x.report("njero ") do counts = Array.new(n) res = Hash.new array.each do |i| counts[i] ? res[i] = nil : counts[i] = true end res.keys end end
Here are the results:
user system total real
hash 0.020000 0.000000 0.020000 ( 0.021919)
divoxx 0.020000 0.000000 0.020000 ( 0.022452)
divoxx2 0.020000 0.000000 0.020000 ( 0.021570)
divnjero 0.020000 0.000000 0.020000 ( 0.014481)
njero 0.010000 0.000000 0.010000 ( 0.012627)
user system total real
hash 0.020000 0.000000 0.020000 ( 0.022935)
divoxx 0.020000 0.000000 0.020000 ( 0.022138)
divoxx2 0.020000 0.000000 0.020000 ( 0.021448)
divnjero 0.020000 0.000000 0.020000 ( 0.016395)
njero 0.010000 0.000000 0.010000 ( 0.012599)
user system total real
hash 0.020000 0.000000 0.020000 ( 0.021925)
divoxx 0.020000 0.000000 0.020000 ( 0.021771)
divoxx2 0.020000 0.000000 0.020000 ( 0.021625)
divnjero 0.020000 0.000000 0.020000 ( 0.015436)
njero 0.010000 0.000000 0.010000 ( 0.014252)
user system total real
hash 0.020000 0.000000 0.020000 ( 0.021945)
divoxx 0.020000 0.000000 0.020000 ( 0.021335)
divoxx2 0.020000 0.000000 0.020000 ( 0.021150)
divnjero 0.010000 0.000000 0.010000 ( 0.018440)
njero 0.020000 0.000000 0.020000 ( 0.012805)
user system total real
hash 0.020000 0.000000 0.020000 ( 0.024188)
divoxx 0.020000 0.000000 0.020000 ( 0.023519)
divoxx2 0.020000 0.000000 0.020000 ( 0.026239)
divnjero 0.010000 0.000000 0.010000 ( 0.015307)
njero 0.020000 0.000000 0.020000 ( 0.014731)
user system total real
hash 0.020000 0.000000 0.020000 ( 0.024547)
divoxx 0.020000 0.000000 0.020000 ( 0.023641)
divoxx2 0.020000 0.000000 0.020000 ( 0.024573)
divnjero 0.020000 0.000000 0.020000 ( 0.014632)
njero 0.010000 0.000000 0.010000 ( 0.014804)
user system total real
hash 0.020000 0.000000 0.020000 ( 0.024099)
divoxx 0.020000 0.000000 0.020000 ( 0.023453)
divoxx2 0.020000 0.000000 0.020000 ( 0.031250)
divnjero 0.020000 0.000000 0.020000 ( 0.014582)
njero 0.010000 0.000000 0.010000 ( 0.015394)
user system total real
hash 0.020000 0.000000 0.020000 ( 0.028281)
divoxx 0.020000 0.000000 0.020000 ( 0.025164)
divoxx2 0.020000 0.000000 0.020000 ( 0.024676)
divnjero 0.020000 0.000000 0.020000 ( 0.014608)
njero 0.020000 0.000000 0.020000 ( 0.015209)
Credits go to Skiz on this one:
require 'lib/scrapi' # http://blog.labnotes.org/2006/07/11/scraping-with-style-scrapi-toolkit-for-ruby/ require 'net/http' html = nil Net::HTTP.start('www.cnn.com', 80) {|http| http.request_get('/') {|res| html = res.read_body } } top_story = Scraper.define do process "a", :description => :text, :url => "@href" result :description, :url end cnn = Scraper.define do array :stories process "div#cnnTopStoriesModule li", :stories => top_story result :stories end stories = cnn.scrape(html) stories.each do |story| puts "<a href=\"http://www.cnn.com#{story.url}\">#{story.description}</a>" unless story.url =~ /^javascript/ end
Here is a fun example of what some of the possibilities are for Javascript! This demo by MooTools shows how you can grab an object and drag it around and watch it bounce all over the screen.
I use PostgreSQL for nearly all of my projects with Rails these days and have encouraged many others to do the same. Now, for those of you currently using it who want to explore the very latest features, you can do so. Here’s how to get the latest beta version:
Download the 8.2 Beta 2 version and give it a test run. Here is a chance to get that edge in using some of the latest features available through a high-quality open source database system.
Good Luck!
Here is a resized version of the diagram and of course for the full version, click on the image or right click and save image as:








