Posts tagged with “Ruby”
- All (24)
- Entries (5)
- Links (19)
- Photos (0)
Ruby Hash Tricks
Some really cool tricks with Ruby hashes–if you supply a block to Hash.new, that will act as a default value for a given key. For example, here’s a Fibonacci hash I put together which caches the values (making it pretty quick):
fibonacci = Hash.new do |h,k|
if k < 2
h[k] = k
else
h[k] = h[k-1] + h[k-2]
end
end
fibonacci[6] # => 8
fibonacci[100] # => 354224848179261915075
The Official Viddler Ruby Gem
I just announced our new Ruby gem over on the Viddler blog. It’s a fairly basic wrapper for our v2 API for now, but I definitely have plans for some really cool features, like having ActiveModel compatible classes for stuff like videos, playlists, users, etc., as well as making it easy to integrate into existing ActiveRecord models in Rails.
So, if you’re into Ruby and you’re looking for a way to integrate video into your site, definitely check us out. We have a really great API, and now that there’s an official gem, it’s easier than ever to get started.
Canable: The Flesh Eating Permission System
John Nunemaker releases his super-simple way to implement permissions in a Rails app. I love how this does away with a complicated role system and just deals with whether or not a user can do specific action. Simplicity at its finest.
Rack::Codehighlighter
A really clever solution to highlighting code through Rack middleware. I’m going to have to replace my current Javascript solution with this, seems a lot less kludgy.
RSpec and Sinatra Quick Start
Quick and easy way to test a Sinatra app with RSpec.
Eight tips for getting the most out of your Rails app
Some good tips on optimizing your Rails site. I’m already using a couple of these on this site, but I definitely want to try out the rest of them as well.
Filestore Cache Expires_in Plugin
Adds an :expires option to filestore caching in Rails–I’ll definitely have to try this out on this site, as standard cache expiration using sweepers and observers is a huge pain.
That’s Not a Memory Leak, It’s Bloat
Excellent post by the team over at EngineYard on common memory issues in Rails, as well as how to fix them. Also be sure to check out the comments below, as there are a lot more suggestions on how to improve your app.
Compiling Ruby, RubyGems, and Rails on Snow Leopard
Dan Benjamin walks through how to install Ruby, RubyGems, and Rails on Snow Leopard. Also be sure to check out his other Snow Leopard guides for Mercurial, Git, and MySQL.
Simple APIs using SerializeWithOptions
Viget Labs rolls out a Rails plugin to cleanup your API’s code.
Instead of doing this in the controller:
@speaker.to_xml(
:methods => [:average_rating, :avatar_url],
:except => [:email, :claim_code],
:include => {
:talks => {
:methods => :average_rating,
:except => :creator_id
}
}
)
You move it to the class like this:
class Speaker < ActiveRecord::Base
serialize_with_options do
methods :average_rating, :avatar_url
except :email,
:claim_code
includes :talks
end
end
Great way to DRY up your code.
Haml/Sass 2.2 Released
Haml/Sass 2.2 was released today, and it’s got a lot of great new features. While I’ve never tried Haml, I use Sass for this site, and I can’t wait to try out 2.2. My favorite update: mixins with arguments
Improving on Related Entries
A little while back, I posted about how I was determining related entries for my site. That method worked, but once I redid my site and added my 250+ Flickr photos, it started to really slow down when finding related photos, because of the increase in tags and posts. The real issue was that I was doing most of the work in Ruby, when it really should have been done with SQL. So, I decided to rewrite it.
Note: If you haven’t looked at my previous entry on the subject, you might want to take a look at it, just for the general idea of what I’m trying to accomplish. Essentially, I’m trying to find related posts by comparing tags. Here’s my new Post#related code:
def related(limit=5)
return [] if tags.empty?
join_array = tags.collect {|tag| "posts_tags.id = #{tag.id}"}
tags_join = "AND (#{join_array.join(' OR ')})"
self.class.find(:all,
:joins => "INNER JOIN taggings posts_taggings ON posts_taggings.taggable_id = posts.id" +
"INNER JOIN tags posts_tags ON posts_tags.id = posts_taggings.tag_id #{tags_join}",
:conditions => ["posts.id != ?", id], :group => "posts.id",
:order => "COUNT(*) DESC",
:limit => limit)
end
You can see, as I mentioned, that all the work is being done in the SQL now. I first create a list of tags from the current post, which I then feed into the query to search for other posts with similar tags. The SQL instructs the database to search for any posts with any of these tags, and then orders them based on how many tags match between the 2 posts. The SQL ended up being fairly complicated, with a lot of joins, but it’s now a whole lot faster, because I’m not creating a lot of overhead by dealing with the computation in Ruby. If you’re interested, here’s an example related entry query:
SELECT `posts`.* FROM `posts`
INNER JOIN taggings posts_taggings ON posts_taggings.taggable_id = posts.id
INNER JOIN tags posts_tags ON posts_tags.id = posts_taggings.tag_id
AND (posts_tags.id = 695
OR posts_tags.id = 192
OR posts_tags.id = 195)
WHERE (posts.id != 4322) AND ( (`posts`.`type` = 'FlickrPhoto' ) )
GROUP BY posts.id
ORDER BY COUNT(*) DESC
LIMIT 5
