Posts

Showing posts with the label Ruby

CouchDB Ruby Libraries

This is a quick list of CouchDB Ruby libraries. Need to connect Ruby to CouchDB? Try one of these! CouchRest - hosted at GitHub, this library by Chris Anderson (jchris) , has a low level component and a high level component. RelaxDB - also hosted at GitHub, this library provides a base class that your models will extend (similar to the high level component provided by CouchRest.) RelaxDB adds pagination support as well as has_many and belongs_to relationship support. CouchObject - this library is unique because it is implemented as a module, to be included in your model class. This library also has both low level and high level components. Basic Model - from topfunky, this library was featured in the PeepCode CouchDB episode . Your model classes extend BasicModel. ActiveCouch - tries to look like ActiveRecord. Supporting a simple find method to query views. Views are defined as Ruby classes, and loaded via rake tasks. CouchFoo - attempts to replication ActiveR...

Sorting By Multiple Conditions in Ruby

I recently had to sort by multiple conditions in Ruby, and had a hard time coming up with the Ruby way to do this. The Ruby Docs didn't have what I was looking for, either. Thankfully, #ruby-lang was very helpful (and confirmed that the docs were lacking here). Let's say you want to sort this array of arrays: a = [[1,2,3],[1,0,2],[2,3,2]] and you want to sort by both the 0 and 1 indexes of the inner arrays. You can write this: a.sort_by{|e| [e[0],e[1]]} You will get this: [[1,0,2],[1,2,3],[2,3,2]] The sort_by method is used when it's costly to do the comparison itself (for instance, if you need to sort File objects, which are costly to create during a normal call to sort ). sort_by creates another enumeration of keys, one for every element in your array to be sorted. In the above example, we are relying on the fact that Array implements the <&eq;> method. So we are creating a new array which contains the multiple elements corresponding to our multiple condi...

Calculating Combinations In Ruby From Erlang

Well, thanks to the many people ( here and here ) that provided their versions of an erlang way to calculate combinations , I've really begun to open my mind to how to think functionally. To help me understand what is going on, I've converted the basic idea into a Ruby version of calculation combinations. This uses recursion like the erlang versions do. class Array def head_tail [self.first, self.tail] end def tail self[1,self.size-1] end end def combos(list) return [[]] if list.empty? h, t = list.head_tail t_combos = combos(t) t_combos.inject([]) {|memo, obj| memo << [h] + obj} + t_combos end c = combos([1,2,3,4]) require 'pp' pp c As you can see, I added a bit of erlangism to the Array class, by adding a method to get the head and tail of an array. Let's run through this. On the first call to combos([1,2,3,4]) we jump over the first line (the exit in our recursion). We generate the head and tail, which in this case is 1 and [2,3...

Calculating Combinations The Cool Way

I recently had to calculate all possible combinations of a set. I needed to calculate combinations of 1..N size, where N is the size of the original set of things. Order inside of the resulting combinations did not matter to me, as I am treating the combinations as true sets. For example, given the set [A,B,C] , I needed to calculate the following combinations: [] [A] [B] [C] [AB] [AC] [BC] [ABC] It dawned on me that a cool way to generate the combinations was to treat the sets (the original set and the resulting combination sets) as bit strings. If the bit corresponding to the member is on, I include the member in the combination. To explain, I start with the set [A,B,C] . I create a number that has three bits, all on, one for each member of the set. I therefore have the binary number 111 matching [A,B,C] . 111 happens to be 7 in decimal, which is one less than the total number of combinations I require. Starting with zero, I loop up and including seven (for a total of eight i...

Comparing Rubyists to Java-ists

...Rubyists tend to function in evangelistic/defensive mode continuously compared to the Java tradition of intense self-criticism... Found in In Relation To...

Another Ruby ETL Project from Google’s Summer of Code

Google's Summer of Code is sponsoring a Framework for ETL and Data mining operations in Ruby . Hmm... sounds a lot like ActiveWarehouse ETL , which is a Ruby library for ETL. ActiveWarehouse-ETL is already in progress and well on its way. Here's the whole list of Ruby projects sponsored by Summer of Code .

Creating Combinations of Sets/Arrays/Things in Ruby

I was looking for a way to create combinations of things in Ruby and I found an article by Uncle Bob detailing his attempt at writing a combination generator in Ruby. I modified it slightly to use an array of items, instead of simple indexes. require 'pp' def choose(n, k) return [[]] if n.nil? || n.empty? && k == 0 return [] if n.nil? || n.empty? && k > 0 return [[]] if n.size > 0 && k == 0 c2 = n.clone c2.pop new_element = n.clone.pop choose(c2, k) + append_all(choose(c2, k-1), new_element) end def append_all(lists, element) lists.map { |l| l << element } end all = [:a, :b, :c, :d] pp choose(all,3) The above code prints out: [[:a, :b, :c], [:a, :b, :d], [:a, :c, :d], [:b, :c, :d]] If you don't want these types of combinations, there is a Ruby library for calculating Permutations which will give you all the different permutations, or orderings, of a set of things.

Installing OpenSSL Support for Ruby on Ubuntu

The more I work with Ubuntu , the more I think it's a very good desktop, but not a good development machine. For instance, you can install Ruby 1.8.4 from the package management system, but not 1.8.5 (or 1.8.6 which is now the latest). So you're stuck compiling ruby on your own. Usually that's not too big of a deal. However, for some reason, the default way of compiling Ruby from source on Ubuntu leaves out the installation of OpenSSL support. I had the development openssl libraries package installed, so that wasn't it. I didn't see any errors in the configure process or during compilation. Turns out, to get OpenSSL to compile and install with Ruby on Ubuntu, you need to follow these steps *after you've installed ruby*: cd ruby_src_dir/ext/openssl ruby extconf.rb make make install Success! That seems a bit harder than it should be, huh?

worldofresources.pdf (application/pdf Object)

Resources on Rails highlights some of the new exciting changes to appear in Rails 1.2. The most exciting is ActiveResource, which CRUDifies models as HTTP resources. Go REST!

ActiveRDF: object oriented RDF in Ruby

ActiveRDF: object-oriented RDF in Ruby is a paper submitted to Scripting for the Semantic Web 2006 . Inside, the authors discuss the challenges and successes with using RDF as a storage backend for applications, much in the same way that RDBMS are currently used. Scripting languages, such as Ruby, offer the best chance for RDF integration, because RDF is so flexible, dynamic, and often untyped. Ruby, because it is so dynamic (you can easily add methods to classes, for instance), is a good way to see where the intersection between an OO scripting language and semantic web technologies mix. I'd like to see integration with transactions, plus explicit support for Redland's contexts. ActiceRDF is a great step forward in learning how deep RDF should go in the application stack.

ActiveRDF

ActiveRDF is a ORM type product for Ruby that maps Ruby objects to RDF stores. It's a very new project, so examples and downloads are lacking. However, apparently it has support for pluggable storage engines. The next step would be to see if I can plug this into Oracle 10g's RDF store. Of course, the real value here is if ActiveRDF supports inferencing. Otherwise, it's a cumbersome ORM product, imho.

Spring 2.0 Gets Scripting Support

It looks like Rob Harrop is now moving scripting into the main tree with support for Groovy, BSF and JRuby. Spring, meet Ruby.

Real Lessons for Rails Deployment

James Duncan Davidson has written up some Real Lessons for Rails Deployment . With the introduction of Switchtower, deploying with Rails is a piece of cake. Read on... > It's these lessons that I want to share with you on this Christmas Eve. I know that some of you will want me to just to say "Hey, here's how you do it when you need xyz...". And, I'll get to those in later essays (this one is already too long at over 2500 words). But for now, the lessons. Later, we'll get to the recipes.

Freezing your Rails when you deploy shared

Freezing your Rails when you deploy shared applications in order to protect your app from changes made by the hosting company. > If you’re running a Ruby on Rails application on a shared host, it’s super-double-plus recommended to freeze your Rails. Freezing your Rails means putting the framework into vendor/rails instead of floating with whatever gems that are installed on the host. Because if you do so, you’ll automatically be upgraded when they are. Not a great thing for a production application to have forced upon itself. > The great news is that this is silly simple. If you’re running 0.14.x or newer, you can simple do rake freeze_gems, and the current gems the system is used are unpacked into vendor/rails. Now the host can update as silly as it wants without affecting your application.

File Uploads with Rails

Sebastian Kanthak has created a very handy file upload utility for Rails called FileColumn . > This library makes handling of uploaded files in Ruby on Rails as easy as it should be. It helps you to not repeat yourself and write the same file handling code all over the place while providing you with nice features like keeping uploads during form redisplays, nice looking URLs for your uploaded files and easy integration with RMagick to resize uploaded images and create thumb-nails. Files are stored in the filesystem and the filename in the database.

Rails Petstore

The Rails Petstore is > an implementation of Clinton Begin's JPetstore that has been developed with the Rails web framework. The aim of this project is to develop a reference application that demonstrates the capabilities of the framework and the best practices that should be followed when developing an application. This is for all you Java people out there who have seen the Petstore application in one form or another over the years. The Petstore is a common Java application implementation used to show how a framework is used "in the real world". It's been implemented many times, and comparing a Java Petstore to a Rails Petstore is very useful.

Ruby off the Rails

Ruby off the Rails is a look at Ruby from a Java developer's point of view. > Ruby on Rails is just one facet of what makes Ruby great, just like EJB is only part of the Java™ enterprise platform. Andrew Glover digs beneath the hype for a look at what Java developers can do with Ruby, all by itself.

On the Rails Again

Danny's Blog has some good perspectives from a Java guy who just went to JavaPolis, but who is also On the Rails Again . He did pick up on a general meme of EoD: > A lot of presentations I saw, about Java EE 5 (GlassFish) but also for instance about Spring, stressed that everyone now focusses on EoD(Ease of Development) and ‘code by exception’, meaning: only having to code the exceptions to the default behaviour. Having just come back from The Spring Experience myself, and having written Ruby on Rails applications for the two weeks prior, I can attest to what Danny experienced. Programming in Rails certainly opens up your eyes to what else is possible in the web programming world. Not all of it is good, mind you. You immediately miss the power of your Java IDE, for one thing. I recommend that every Java web developer go code a quick application in Rails, even if they will never use Rails again. It's so easy to get caught up in a language's constraints, both physica...

Ruby to JMS Bindings

You can now integrate Java and Ruby with JMS for Ruby . This implementation uses ActiveMQ as the JMS server. This rocks.

Rails and Allowing ID Editing from Forms

Need to allow yours users to directly edit the ID of your model object? By default, Rails will strip out the ID field from the hash of request parameters. This is a security measure, prohibitings the web interface from altering an object's ID. Generally, this is a good practice. However, some legacy schemas have IDs that are not simply numbers. In those cases, the ID is created and managed by the user. To tell Rails to allow the ID to be edited, you can use attr_protected and attr_allowed . More information is available at http://wiki.rubyonrails.com/rails/pages/HowToEnsureValidAttributesInFormData