ruby

Where does a rails (3) application define what is supposed to happen in activesupport-3.0.0.rc/lib/active_support/testing/setup_and_teardown.rb?

I generated a model file but then chose to let it single table inherit from another model. I had to run no migration, because the columns all were already there. Now whenever I want to run tests, I get complaints that the table for the model does not exist. So I have Product < Article < ActiveRecord::Base, product has no own table (doe...

Serializing a MongoDB grid ID into a string using ActiveRecord

In my Sinatra app, I'm using a MongoDB with Grid to store book covers on Heroku. I want to be able to associate these with the books in my ActiveRecord-driven primary database. Currently, I'm downloading the image from Google Books, storing it in the MongoDB, and storing the BSON::ObjectID object into the database as a string. When I go...

Is there a supplementary guide/answer key for ruby koans?

I have recently tried sharping my rails skills with this tool: http://github.com/edgecase/ruby_koans but I am having trouble passing some tests. Also I am not sure if I'm doing some things correctly since the objective is to just pass the test, there are a lot of ways in passing it and I may be doing something that isn't up to standard...

Is it possible to run Mongo client shell query/commands from Ruby?

Could I use the Mongo client shell queries/commands from inside Ruby? I know there is the Ruby driver DSL, but I was thinking about something similar to running a SQL query from within PHP. Just for the sake of knowing. ...

Which Array method should I overwrite?

I would like to extend an Ruby Array object, to make it returns Array.new if it's nil. My solution: Overwrite [] and at. module NullSafeArray def at(index) value = super return Array.new if value.nil? value end def [](index) value = super return Array.new if value.nil? value end end The problem: Th...

Character encoding in string

I try to output the german setence containing the letter "ü" in escaped form (ascii 252, octal 374, hex 0xfc) using the following code: pp "Test \374" pp "Test \374".encode("UTF-8") But using ruby 1.8.7 I get: "Test \374" "Test \374" Using ruby 1.9.2 outputs: "Test \xFC" "Test \xFC" How can I get ruby (1.8.7 + 1.9.x) to output "Test...

Syntax error, unexpected '='

I have the following as part of a class def to_s i = 0 first_line? = true output = '' @selections.each do | selection | i += 1 if first_line? output << selection.to_s(first_line?) first_line? = false else output << selection.to_s end if i >= 5 output << "\r" ...

How do I invoke methods/access properties of an OCX control via Ruby?

I am writing the scripted GUI tests for a system which I do not have access to its source code; and I want to invoke the methods and access the properties of an OCX control (specifically the MSFlexGrid control) as I could not find any other way of finding out the number of rows and columns, as well as the data values in each cell, in the...

Calling script on image upload in Drupal

Hi all, I would like to call a Ruby script when a user uploads an image to my Drupal content-type. I have a CCK image field that serves as the main image, and ImageCache takes care of resizing and creating thumbnails for me. I have a Ruby script that does some transformations to the image, however I don't exactly know how to call it (...

Can I use controller methods in a observer?

I need to refer to a controller method from a cache observer, How can I make it? ...

Best way to manage date objects?

I have some date objects that are Date of the Date class, and others that are Time. The trouble is is that when I parse them to load them chronologically, they list first as the Times, followed by the Dates. This is the code that loads them : query.any_of do |any_of| any_of.with(:date).greater_than((params[:current_date] || Date...

How do I iterate through a hash where I can view the next key also?

I have a hash of dates to money, and I need to put new money deposits inbetween a set of dates. so, lets say my hash is {"000000001" => "0.00", "000000002" ="12.34", "000000010" => "5.95"} and I want to insert 000000008, 54.34 then my resulting hash should be {"000000001" => "0.00", "000000002" ="66.68", "000000010" => "5.95"} *...

How can I grab certain values in a hash in Ruby?

I have a hash hash = { 1=> { 0=> 'apple', 1=> 'tree'... ....}, 2=> {.....}} I want to grab the 0 for all hashes within the hash. I know there is a transpose for array, but there any way to do this with a hash easily? ...

Does --css-dir always have to be relative to the project directory?

I'm using compass 0.10.4 I'm running a compass command and I want it to put it's output to the server when I run a watch instead of to the project directory. Is there any way of specifying an absolute path instead of a path relative to the directory you want the css output to? I'm trying to have my compiled css output to the directory...

Are Sequel::Model validations only instance methods?

In modern versions of ActiveRecord you can define any number of before_validation handlers using a simple declaration: class MyModel < ActiveRecord::Base before_validation :do_something before_validation :do_something_else end Using Sequel it looks like the only way you can do this is similar to the older ActiveRecord method: cla...

Nokogiri and XPath help

Admittedly, I'm a Nokogiri newbie and I must be missing something... I'm simply trying to print the author > name node out of this XML: <?xml version="1.0" encoding="UTF-8"?> <entry xmlns:gd="http://schemas.google.com/g/2005" xmlns:docs="http://schemas.google.com/docs/2007" xmlns="http://www.w3.org/2005/Atom" gd:etag=""> <category te...

Ruby Bundler Permission issue

So, I'm positive I once did a 'sudo' bundle install' out of desperation, which I now understand is a no-no. But now, when i try to run 'bundle install', I get the following error: Installing culerity (0.2.12) /Library/Ruby/Site/1.8/rubygems/installer.rb:294:in `generate_bin': You don't have write permissions into the /Library/Ruby/Gems...

Ruby on Rails: Problem adding Transient Attribute to Object for JSON Serializaton

Hi everyone, In my Item controller, I wish to add a transient (i.e. non-persistent) attribute to my model object before rendering it as JSON. def show @item = Item.find(params[:id]) @item.comment = "some comment" render :json => @item end My Item class looks like this: class Item < ActiveRecord::Base attr_acc...

Array slicing in Ruby: looking for explanation for illogical behaviour (taken from Rubykoans.com)

I was going through the exercises at http://rubykoans.com/ and I was struck by the following Ruby quirk that I found really unexplainable: array = [:peanut, :butter, :and, :jelly] array[0] => :peanut #OK! array[0,1] => [:peanut] #OK! array[0,2] => [:peanut, :butter] #OK! array[0,0] => [] #OK! array[2] => :and #OK! array[2,2] ...

best language platform for creating product comaparison site

hi i want to create product comparison site expecting good no of user hits (100 + concurrent users) i am from dot net background(LOB Application),i haven't done much research for good programming platform for such user oriented website with some user generated content , i have following options 1: c# asp.net Ajax 2: silver light 3: ph...