ruby

Using Amazon AWS to create an offline database

Hi, Problem Statement: I would like to create an offline database to lookup prices/info on the n most useful books to sell in the United States (where n is probably 3 million or so). Question: So, my question is (and I am open to other approaches here as well), I am trying to figure out how to use Amazon AWS to download a list of the ...

List comprehension in Ruby

To do the equivalent of Python list comprehensions, I'm doing the following: some_array.select{|x| x % 2 == 0 }.collect{|x| x * 3} Is there a better way to do this...perhaps with one method call? ...

What is the "right" way to iterate through an array in Ruby?

PHP, for all its warts, is pretty good on this count. There's no difference between an array and a hash (maybe I'm naive, but this seems obviously right to me), and to iterate through either you just do foreach (array/hash as $key => $value) In Ruby there are a bunch of ways to do this sort of thing: array.length.times do |i| end ar...

Ruby on Rails 2.1 Subdomain-Cookie issues

When using a subdomain and trying to view anything related to current_user. user is sent to a new session page, the page shows the session is created and gives the option to logout. I can use no subdomain and it works fine. ...

How can I put (parts of) a large object graph in Rails.cache?

I have a class in which looking up an instance is expensive, so instances are cached: class Foo def self.find(id) Rails.cache.fetch("Foo.#{id}") do // some expensive lookup, like an HTTParty request, or a long SQL query ... end end end That works fine until Foos have related Foos: class Foo def children ...

How do I run a rake task from Capistrano?

I already have a deploy.rb that can deploy my app on my production server. My app contains a custom rake task (a .rake file in the lib/tasks directory). I'd like to create a cap task that will remotely run that rake task. ...

Routing without namespace in Rails (2.2)

Related to the question asked here -- http://stackoverflow.com/questions/182040/default-segment-name-in-rails-resources-routing. Having trouble in edge rails trying to generate resources without a namespace prefix (i.e. /:apple_id/oranges/). Using the plugin gives me //:apple_id/oranges? Any easier way to do this? Maybe a 2.2 issue? ...

Python equivalent of continuations with Ruby

What is the Python equivalent of the following code in Ruby? def loop cont=nil for i in 1..4 puts i callcc {|continuation| cont=continuation} if i==2 end return cont end > c=loop 1 2 3 4 > c.call 3 4 Reference: Secrets of lightweight development success, Part 9: Continuations-based frameworks ...

Worker threads in Ruby

Hi, I am writing a simple memory game using ruby + qt (trying to get away from c++ for while...) In order to allow a X second timeout to view two open pieces, I need either timers or do the work in a background thread. What is the simplest way of implementing this without reinventing the wheel? Ruby threads? Qt threads? Qt timers? ...

What's the cleanest way to override ActiveRecord's find for both models and collections?

I have library code that overrides Ar's find method. I also include the module for all Association classes so both MyModel.find and @parent.my_models.find work and apply the correct scope. I based my code off of will_paginate's: a = ActiveRecord::Associations returning([ a::AssociationCollection ]) { |classes| # detect http://dev.rub...

Rails model without database

I want to create a Rails (2.1 and 2.2) model with ActiveRecord validations, but without a database table. What is the most widely used approach? I've found some plugins that claim to offer this functionality, but many of them don't appear to be widely used or maintained. What does the community recommend I do? Right now I am leaning ...

rspec: How to stub an instance method called by constructor?

class A def initialize @x = do_something end def do_something 42 end end How can I stub do_something in rspec, before the original implementation is called (thus assigning 42 to @x)? And without changing the implementation, of course. ...

RoR: Best way to build 'dashboard' type page?

In the admin section of a website i am building i would like to put together a dashboard page, or 'quick look' type page where the most recent changes/additions/etc in several different areas can be viewed. I was thinking the best way to do this would be to use partials and have each partial contain the markup for the data i need to dis...

What is the difference between include and require in Ruby?

My question is similar to this one over here about include and extend. What's the difference between require and include in Ruby? If I just want to use the methods from a module in my class, should I require it or include it? ...

RoR: named_scope, all records created within last 7 days?

how do i define a named_scope to return all the records that were created within the last 7 days, and then how do i use that named scope in a controller? ...

get scriptaculous helper drop_receiving_element to not generate "<script>" tags

Is there a way to get drop_receiving_element to not generate "// .. ...

Private module methods in Ruby

I have a two part question Best-Practice I have an algorithm that performs some operation on a data structure using the public interface It is currently a module with numerous static methods, all private except for the one public interface method. There is one instance instance variable that needs to be shared among all the methods. ...

#inject and slowness

I've often heard Ruby's inject method criticized as being "slow." As I rather like the function, and see equivalents in other languages, I'm curious if it's merely Ruby's implementation of the method that's slow, or if it is inherently a slow way to do things (e.g. should be avoided for non-small collections)? ...

Ruby: defining class level hash with default values

i have a basic ruby class: class LogEntry end and what i would like to do is be able to define a hash with a few values like so: EntryType = { :error => 0, :warning => 1, :info => 2 } so that i can access the values like this (or something similar): LogEntry.EntryType[:error] is this even possible in Ruby? am i going about this...

Ruby on Rails and Active Record standalone scripts disagree on database values for :timestamps

I posted a question earlier today when I'd not zeroed in quite so far on the problem. I'll be able to be more concise here. I'm running RoR 2.1.2, on Windows, with MySQL. The SQL server's native time zone is UTC. My local timezone is Pacific (-0800) I have a model with a :timestamp type column which I can do things like this with: ...