ruby

loading/unloading/updating class in ruby

I did a little experiments with Ruby class dynamic loading/unloading/updating as implementing plugins infrastructure. I found a few points: If loading new version of same class without first unloading it, the new one essentially 'top' or 'merge' with previous version. All existent objects created with previous version would get their c...

How many Ruby threads is too many?

I'm coding a Merb application which uses a combination of SimpleDB and Tokyo Tyrant for storage. For both of these data stores I'm implementing IN (list) type-functionality by spinning up a thread for each value in list and then merging the result sets. Bearing in mind that this is a web application, is there a limit to the number of thr...

Does Ruby auto-detect a file's codepage?

If a save a text file with the following character б U+0431, but save it as an ANSI code page file. Ruby returns ord = 63. Saving the file with UTF-8 as the codepage returns ord = 208, 177 Should I be specifically telling Ruby to handle the input encoded with a certain code page? If so, how do you do this? ...

schema.rb not updating after transfer to a new machine

I transfered a project to a new machine. Everything works. I can run migrations and they update the mysql database. However, the schema.rb file doesn't acknowledge the changes. I checked the read/write permissions for schema.rb are OK. Does anyone have any idea about what could cause this problem. I'm using Rails version 2.3.5. ra...

When is `eval` in Ruby justified

Another question inspired this one: Mostly everybody agrees that eval is bad, and in most cases there is more elegant / safer replacement. So I wanted to ask: if eval is misused that often, is it really needed as a language feature? Is it doing more evil than good? Personally, the only place I find it useful is to interpolate strings ...

Installing gems from behind a corporate firewall

I suspect that the corporate firewall is preventing gems from getting installed. I have HTTP_PROXY defined and I'm able to view remote gems via the following command: jruby -S gem list -r But when I go to install a gem, I get a 404: jruby -S gem install rails Is there a good workaround for resolving this issue other than maintainin...

Accessing a class variable overloading brackets [] operators method in Ruby

Hi i want to do the following. I simply want to overload the [] method in order to access the instance variables... I know, it doesn't make great sense at all, but i want to do this for some strange reason :P It will be something like this... class Wata attr_accessor :nombre, :edad def initialize(n,e) @nombre = n @...

pulling webpages from an adult site -- how to get past the site agreement?

I'm trying to parse a bunch of webpages from an adult website using Ruby: require 'hpricot' require 'open-uri' doc = Hpricot(open('random page on an adult website')) However, what I end up getting instead is that initial 'Site Agreement' page making sure that you're 18+, etc. How do I get past the Site Agreement and pull the webpag...

ActiveRecord: filtering children without hitting the database

I'm looking for a clean way to filter the children of a parent in a has_many relationship without hitting the database and thus reloading the db's view of the objects back into the app. For example: class Parent < ActiveRecord::Base has_many :children, ... ... end My understanding (correct me if I'm wrong) is that parent.childr...

Ruby url parsing - determine whether one url is under the same folder

I'm looking for a method that will allow me to take two URL's and decide if one is in the scope of the other, meaning that it is either in the same folder or in a subdirectory beneath it. I could write the method myself if I had to, just wondering if anyone knew of something already in the ruby libraries. Ex. of what I mean: url1 = 'htt...

Rails comparing dates

Hi there, I have a data model that contains a DateTime attribute, date. I want to find out if that date is today. I'm having a hell of a time! >> Deal.first.date => Mon, 14 Dec 2009 23:59:59 EST -05:00 I've tried so many techniques that I would be embarrassed to put them here. So I would just ask, what do I need to match this date in...

How to calculate how many years passed since a given date in Ruby?

This question was here for other languages, so let here be one for Ruby. How do I calculate number of complete years that have passed from a given date? As you probably have guessed, that's to calculate person's age automatically. The closest one is distance_of_time_in_words Rails helper, so the following template Jack is <%= distanc...

Functional Testing of Authorization In Rails

I know how to run functional/integration tests in Rails, this question is about best practices. Let's say authorization is performed using four distinct user roles: basic editor admin super This means that for each action there are up to five different behaviors possible (4 roles + unauthenticated/anonymous). One approach I've taken ...

Twitter List Membership Count Per User

The Twitter API has the friends_count and followers_count available as cached values for the users/show or account/verify_credentials method. As far as I can tell, the only way to determine the number of lists a user is a member of is to make a call to GET list memberships and paginate through using the cursor to count the total number ...

Best dynamic languages for OpenGL/general graphics

Which are the most mature and well supported solutions for writing graphical programs? I have been using C++ with OpenGL/GLUT, but would like to try a more flexible and expressive approach. Ruby and Processing? Python and OGRE? What things have worked well for you? ...

String operation in ruby for credit card number

Working on a rails project where there's an order confirmation string with a credit card number with all but the last four digits starred out. What's the proper way to do a string substitution? What's the operation to get this credit_card_number = "1111111111111111" to this? credit_card_number = "************1111" Thanks, Kenji ...

ruby on rails add functionality to model property change

In my rails model, I have a decimal property called employer_wcb. I would like it if, when employer_wcb was changed, a dirty bit was set to true. I'd like to override the employer_wcb setter method. Any way to do so (in particular using metaprogramming)? ...

Change Windows browser proxy settings via ruby script

I'm looking for some code/library to programmatically change proxy settings for popular browsers using Ruby on Windows. Thanks. ...

Loading an hpricot element with a chunk of html

is there a way to load a chunk of html into an Hpricot::Doc object? I am trying to parse various chunks of html within custom tags from a page. so if I have: <foo> <b>here is some stuff</b> <table> <tr> <td>one</td> <td>two</td> </tr> <tr> <td>three</td> <td><four</td> </tr> </table> </foo...

Validating an Excel file in a Rails app

I have a Rails app which allows users to upload Excel files which need to be validated according to some pre-defined rules. For eg. sheet 1 contains two columns, a numerical id and a name; sheet 2 contains three other columns etc.. Are there any Ruby gems/libraries that can help in doing these validations? ...