Archive for the 'Ruby' Category

rQRCode, a Ruby library for encoding QR Codes

February 24th, 2008 @ 15:37 comments(8)

[Update]
I have also posted a bit more about the upcoming QR Code work for BBC /programmes.

We implemented QR Codes on /programmes (A project I’m one of the Software Engineer’s on, at the BBC) a few weeks back. They’ve been talked about since so I won’t repeat things, only to say it was a simple implementation using a JavaScript library by Kazuhiko Arase and took all of 5 minutes of my lunch hour to add. It started as an email by colleague Michael Smethhurst asking if we could/should implement them, and was also the first I had really heard of them.

Since then, I have done lots more research and think there is great mileage there for promotional material and advertising. At the moment the codes are built on the client side, but this will be moved to the server soon so we can start caching the pages, and also so we can provide the code in a more useful format (maybe images instead of the current HTML table).

So to the point of this post. During my research to understand QR Codes more and because of the lack of readable spec (Anyone know if there is one?) I decided to reverse engineer the JavaScript library into a Ruby Gem as there didn’t seem to be one out there. This helped me understand the how QR Codes are encoded, while at the same time giving something back for other people to use.

Anyway, it’s now done and there is a rQRCode Rubyforge project setup. You can also view the rRQRCode documentation (still to be improved) or just jump straight into installing the gem.

A quick simple example in the console:

Also, here is a quick example of using it in a Ruby on Rails project:

So that should get you going. I’m have some interesting features I working on adding so expect lots of updates over the next few weeks. Enjoy!

Filed Under: BBC, Ruby, gems, qrcodes

Show Us Your Gems

February 12th, 2007 @ 16:12 comments(0)

What a good idea. Here’s what I have installed currently:

$ gem list|grep '^[a-zA-Z]'
actionmailer (1.3.3, 1.3.2)
actionpack (1.13.3, 1.13.2)
actionwebservice (1.2.3, 1.2.2)
activerecord (1.15.3, 1.15.2)
activesupport (1.4.2, 1.4.1)
acts_as_versioned (0.2.3)
amazon-ec2 (0.1.0)
builder (2.0.0)
camping (1.5.180)
camping-omnibus (1.5.180)
capistrano (1.99.1, 1.4.1, 1.4.0)
cgi_multipart_eof_fix (2.1)
daemons (1.0.4)
deprec (1.3.1)
fastercsv (1.2.0)
fastthread (0.6.3)
ferret (0.11.2)
flexmock (0.6.1, 0.6.0, 0.5.0)
gem_plugin (0.2.2)
highline (1.2.7)
hoe (1.2.0)
hpricot (0.5)
map_by_method (0.6.0)
markaby (0.5)
metaid (1.0)
mocha (0.4.0)
mongrel (1.0.1)
mysql (2.7)
needle (1.3.0)
net-sftp (1.1.0)
net-ssh (1.1.1, 1.1.0, 1.0.10)
railroad (0.3.3)
rails (1.2.3, 1.2.2)
railsbench (0.9.2)
rake (0.7.2, 0.7.1)
rb-appscript (0.3.0)
RedCloth (3.0.3)
rfacebook (0.6.2)
rmagick (1.15.6, 1.15.2)
rspec (0.8.2, 0.7.5.1)
ruby-openid (1.1.4)
ruby-yadis (0.3.4)
rubyforge (0.4.1)
sources (0.0.1)
sqlite3-ruby (1.1.0.1)
stemmer (1.0.1)
termios (0.9.4)
test-spec (0.3.0)
what_methods (1.0.1)
xml-simple (1.0.10)

Filed Under: Ruby

Shuffling an Array in Ruby

November 10th, 2006 @ 12:40 comments(0)

UPDATE Someone called NoKarma and perraultd have posted even nicer versions on the Code Snippets site, thanks!:

I had a situation when I need to shuffle the contents of an array to randomize the stuff inside. After hunting and hacking the final solution appeared to be:

So you just enhance the Array Class and give it a new funky method shuffle! so you can do stuff like:

Just how cool is Ruby.

Filed Under: Open source, Ruby

Ruby one liners via the command line

July 23rd, 2006 @ 11:18 comments(0)

David P Thomas has been collecting Ruby one-liners and sticking them in a single text file for us all to enjoy. The list looks like it hasn’t been touched in a while but has has proven to be very useful for me.

Shame it’s in a .txt file as finding the ones that would be helpful requires memorising them all, or searching through and hoping to find the one you’re after. Great stuff all the same though.

Filed Under: Ruby, programming

Another tag cloud script for Ruby on Rails

July 6th, 2006 @ 09:09 comments(3)

Or any other Ruby program really. I know there are attempts already out there but none that really suited my purposes. I needed a simple way of creating that tag-cloud-look for many situations. I needed a tag cloud by size or by colour and I didn’t want it to do everything, just provide me with the style attributes I needed to get the job done. Below is an example of what gets returned by default:

font-size:16px;color:rgb(70,70,70);

Here’s the code; it comes with no warranty.

With all these tags cloud scripts you really only need 3 parameters:

  1. The total counts of items - For example you have 10 tags each that has been used numerous times. The total counts would be all of the uses added together
  2. The minimum count of items in a batch - Using the example above this would be a count of the uses for the tag that has been used least
  3. The maximum count of items in a batch - Using the example above this would be a count of the uses for the tag that has been used most

Below is the actual script and then below that I have stuck a quick example of how you could use it.

def font_size_for_tag_cloud( total, lowest, highest, options={} )
 return nil if total.nil? or highest.nil? or lowest.nil?
 #
 # options
 maxf = options.delete( :max_font_size ) || 14
 minf = options.delete( :min_font_size ) || 11
 maxc = options.delete( :max_color ) || [ 0, 0, 0 ]
 minc = options.delete( :min_color ) || [ 156, 156, 156 ]
 hide_sizes = options.delete( :hide_sizes )
 hide_colours = options.delete( :hide_colours )
 #
 # function to work out rgb values
 def rgb_color( a, b, i, x)
  return nil if i <= 1 or x <= 1
  if a > b
   a-(Math.log(i)*(a-b)/Math.log(x)).floor
  else
   (Math.log(i)*(b-a)/Math.log(x)+a).floor
  end
 end
 #
 # work out colours
 c = []
 (0..2).each { |i| c << rgb_color( minc[i], maxc[i], total, highest ) || nil }
 colors = c.compact.empty? ? minc.join(',') : c.join(',')
 #
 # work out the font size
 spread = highest.to_f - lowest.to_f
 spread = 1.to_f if spread <= 0
 fontspread = maxf.to_f - minf.to_f
 fontstep = spread / fontspread
 size = ( minf + ( total.to_f / fontstep ) ).to_i
 size = maxf if size > maxf
 #
 # display the results
 size_txt = "font-size:#{ size.to_s }px;" unless hide_sizes
 color_txt = "color:rgb(#{ colors });" unless hide_colours
 return [ size_txt, color_txt ].join
end

and now a quick example of it’s use in a rails view. This code assumes the db find call looks something like this:


@foo = Foo.find( :all,
:select => '*, COUNT(DISTINCT(i.id)) AS item_cnt',
:joins => 'AS f INNER JOIN bars AS b ON f.id=b.foo_id',
:group => 'b.id',
:order => 'item_cnt DESC',
:limit => 50
)

Below just loop through your db results and calls font_size_for_tag_cloud for each item, passing in the required parameters. There are a few options available but if you leave them out you should still see something useful.


<ul>
<% for f in @foo -%>
<%= content_tag( 'li', f.some_label, :style => font_size_for_tag_cloud( f.bars.size,
f.first.count_items,
f.last.count_items,
) ) %>
<% end -%>
</ul>

That’s it! I guess I should turn it into a plugin but hey, there’s to much reading to be done on ActiveResource at the moment heyhey!

Filed Under: Ruby on Rails, Ruby

Back to top of page