ruby

Ruby: Object.to_a replacement

I need to convert a passed in argument (single object or collection) to an Array. I don't know what the argument is. If it is an Array already, I want to leave it, otherwise create a one-element array from it. I'm looking to allow both method(:objs => obj) and method(:objs => [obj1, obj2]) This seems to be the best way (Array.to_a ret...

To use self. or not.. in Rails

I've been coding in Ruby for sometime now, but I don't understand when to use: def self.METHOD_NAME end or just: def METHOD_NAME end In any Rails model. Is "self" a modifier like private in Java? When should I use it and when not?. Thanks a ton. ...

best way to handle user id hash common to all rails requests

Each client is identified by a hash, passed along with every request to the server. What's the best way to handle tracking a users session in this case? I'm using restful_authentication for user accounts etc. A large percentage of requests are expected to originate without a user account but just the unique hash. My understanding of th...

Constants or class variables in ruby?

I've been programming in Ruby for a few months now, and I'm wondering when it is appropriate to use constants over class variables and vice versa. (I'm working in Rails, thinking about constants in models). class Category TYPES = %w(listing event business).freeze end OR class Category @@types = %w(listing event business).freeze ...

Spinning Background Tasks in Rails

What is the preferred way to create a background task for a Rails application? I've heard of Starling/Workling and the good ol' script/runner, but I am curious which is becoming the defacto way to manage this need? Thanks! Clarification: I like the idea of Rake in Background, but the problem is, I need something that is running constan...

Using Erubis 2.6.2 with Rails 2.2.2 is incompatible?

Supposedly installing erubis is as simple as: gem install erubis # And in environment.rb: require 'erubis/helpers/rails_helper' But I haven't found this to be so. Note that there are no evident errors in my code; it runs just fine and dandy with ERB. Where do I put this line? Directly after the boot.rb inclusion it fails to start...

Should I learn Rails or Merb?

Background: After taking time to reflect on the answers to a question I asked just yesterday regarding which web-focussed language to learn for a beginner (link) I decided that I would most likely pursue learning Ruby/Rails (since I had already made it 1/3 of the way through a book on it about a year ago and felt like I was able to under...

Installing Ruby 1.8.7 (and other stuff) manually

Hi, I don't want to rely on one-click installer anymore and I want to learn how to install ruby manually. Do you have any resource for this? Thanks a lot. Cheers ...

uploading a file to a website with ruby/rails

I am building a rails app to test our flagship product (also web based). The problem is that part of the testing requires using the production app's web interface to upload files. So what i need to do is have the rails app upload these files to the production application (not rails). Is there a way to have rails post the file to the p...

How can I translate Perl's Convert::ASN1 to Ruby code?

Can somebody advise me what this code does and how can I convert it to Ruby in most simple way? #!perl use Convert::ASN1; my $asn1 = Convert::ASN1->new(encoding => 'DER'); $asn1->prepare(q< Algorithm ::= SEQUENCE { oid OBJECT IDENTIFIER, opt ANY OPTIONAL } Signature ::= S...

How do you make a window to fill the entire screen?

Can someone give me a pointer on how to make a window in Shoes to fill the entire screen? I am looking to have a window that is akin to Writeroom. Thanks. ...

Is there a Ruby/Python HTML reflow/layout library?

I'm looking for a library in Ruby or Python that would take some HTML and CSS as the input and return data that contains the positions and sizes of the elements. If it helps, I don't need the info for all the elements but just the major divs of the page. ...

What’s Your Favorite Feature in Ruby?

Similar questions have been asked for .NET and Java, but not for Ruby. So, what’s your favorite feature in Ruby? You might also be interested in hidden features of Ruby. Please be specific and post one feature per answer. An explanation or a code sample would be nice. ...

Ruby Amazon book search

Does anybody have a good ruby script for finding books (or other products) on Amazon using their API? ...

How to call expire_fragment from Rails Observer/Model?

I've pretty much tried everything, but it seems impossible to use expire_fragment from models? I know you're not supposed to and it's non-MVC, but surely there much be some way to do it. I created a module in lib/cache_helper.rb with all my expire helpers, within each are just a bunch of expire_fragment calls. I have all my cache...

Refactor with some dynamic programming?

I have a piece of code here that i really could use some help with refactoring. I need the different methods for adding relational data in a form in rails. The code is taken from http://railscasts.com/episodes/75-complex-forms-part-3, my problem is that i need to have the methods fro both the Material model and the Answer model. So i nee...

What does 'Monkey Patching' exactly Mean in Ruby?

According to Wikipedia, a monkey patch is: a way to extend or modify the runtime code of dynamic languages [...] without altering the original source code. The following statement from the same entry confused me: In Ruby, the term monkey patch was misunderstood to mean any dynamic modification to a class and is often ...

How do you talk SQL directly to MySQL from Ruby?

I want to write a script in Ruby to clean up some messed up keys in several copies of the same MySQL schema. I'd like to do something like SHOW CREATE TABLE, then look at what comes back and delete keys if they exist. I know in the Rails environment you can do this... ActiveRecord::Base.connection.execute( some sql ) But what you get...

Extraordinarily Simple Ruby Question: Where's My Class?

[I'm just starting with Ruby, but "no question is ever too newbie," so I trudge onwards...] Every tutorial and book I see goes from Ruby with the interactive shell to Ruby on Rails. I'm not doing Rails (yet), but I don't want to use the interactive shell. I have a class file (first_class.rb) and a Main (main.rb). If I run the main.rb, I...

Trouble sorting records on unique attributes..

Working with the following code, I need to return only records where the `point' attribute is unique. I can't seem to get there by myself. uniques = Item.find_all_by_item_id(item_id) uniques.sort! {|a, b| b.point <=> a.point } # how do I reject the equal points? In other words.. I guess, how do you make [0, 1, 1, 1, 2, 3, 3, 4, 4, 7]...