ruby

Ruby: How do I get the target of a symlink?

I have a string containing the file system path to an existing symlink. I want to get the path that this link points to. Basically I want the same that I'd get through this bit of hackery: s = "path/to/existing/symlink" `ls -ld #{s}`.scan(/-> (.+)/).flatten.last but I want to do it without shelling out. ...

Ruby Webrick HTTP Authentication

How can I do the same authentication stuff in this page using a subclass like this: class Configuration < HTTPServlet::AbstractServlet def do_GET (request, response) SOMETHING.... end end server = HTTPServer.new(:Port => 666) server.mount "/conf", Configuration trap "INT" do server.s...

How do I specify the shell to use for a ruby system call?

I am trying to run commands from ruby via system (or by using backticks), but am running into problems. When I try to call a command, the shell is unable to find it, even though I know it works if I call it straight. For example: `zip` >> sh: zip: command not found The problem seems to be that ruby is using the sh shell, in which $PAT...

Rails group by year problem

I have a Post model which includes a date_published. I want to display links on the index page to view all the posts of a certain year (a bit like where blogs have months you can view by) The only way I can think of returning the years is by iterating over all of the posts and returning a unique array of the years that posts fall in. Bu...

Listing directories at a given level in Amazon S3

I am storing two million files in an amazon S3 bucket. There is a given root (l1) below, a list of directories under l1 and then each directory contains files. So my bucket will look something like the following l1/a1/file1-1.jpg l1/a1/file1-2.jpg l1/a1/... another 500 files l1/a2/file2-1.jpg l1/a2/file2-2.jpg l1/a2/... another 500 file...

First Rails Project: Rake Problem

I'm running rails on Mac OS X. I think I installed it correctly, but I'm getting the following error. $ rake db:create (in /Users/user_name/myapp) rake aborted! Could not find RubyGem mocha (>= 0) (See full trace by running task with --trace) What is the problem? How do I fix it? ...

Different 'upto' behavior in ruby on difference machines (`upto': no block given (LocalJumpError))

This code works on one machine but not the other: puts 1.upto(5) On the working machine, the code returns '#'. On the other machine, I get this error: test.rb:1:in `upto': no block given (LocalJumpError) from test.rb:1 Both machines have rails 2.2.2. The machine where this code works has ruby 1.8.7, while the two machines where...

Is there a name to this Ruby on Rails common model pattern? Polylink?

There seems to be no name for this common model pattern. It's used in many plugins like acts_as_taggable[_whatever] and it basically allows linking a certain model, like Tag, with any other model without having to put ever-more belongs_to statements in the Tag model. It works by having your model (Tag) linked to a polymorphic join mo...

Using Jeremy Mcanally's context for testing in Rails 2.3.x

I'd like to try out Jeremy Mcanally's context gem for testing: http://github.com/jeremymcanally/context/tree/master It does not work out of the box with Rails 2.3.x, anyone got it working? EDIT hi mike, tx for ur help. i installed the gem like u said, and have added "require 'context'" to test_helper.rb however, it stil doesn't seem t...

Regex: Match a string containing numbers and letters but not a string of just numbers.

Question I would like to be able to use a single regex (if possible) to require that a string fits [A-Za-z0-9_] but doesn't allow: Strings containing just numbers or/and symbols. Strings starting or ending with symbols Multiple symbols next to eachother Valid test_0123 t0e1s2t3 0123_test te0_s1t23 t_t Invalid t__t ____ 0123012...

Foreign keys in fixtures in rails 2.0

I'm having problems with my fixtures in Rails. I have two models, a message and a user. The user model is generated from restful authentication. A message has a sender and a recipient, which are both users. When I don't use the fixtures everything works fine. But when I try to use them in fixtures both are nil. This is the messages fi...

How do I convert a Proc to a block in a Ruby C extension?

I am storing an array of procs in a Ruby C extension and I need to go through and instance_eval each proc. The problem is that instance_eval only accepts blocks, not procs. This is not an issue in Ruby where I can simply go: proc_list.each { |my_proc| @receiver.instance_eval(&my_proc) } However I am unsure how to go about this usi...

A copy of ApplicationController has been removed from the module tree but is still active!

Whenever two concurrent HTTP requests go to my Rails app, the second always returns the following error: A copy of ApplicationController has been removed from the module tree but is still active! From there it gives an unhelpful stack trace to the effect of "we went through the standard server stuff, ran your first before_filter on...

Xen API Ruby Bindings?

Does anyone know if there is a full XenServer API implementation in ruby floating around out there? I found this: http://github.com/rubiojr/pangea/tree/master but its read only and not fully built out. ...

Pros/Cons of MySQL vs Postgresql for production Ruby on Rails environment?

I will soon be switching from sqlite3 to either postgres or mysql. What should I consider when making this decision? Is mysql more suited for Rails than postgres in some areas and/or vice versa? Or, as I somewhat suspect, does it not really matter either way? Another factor that might play into my decision is the availability of tools t...

text parsing in ruby

Hi..I am a naive programmer in ruby..just learing to write hello worlds using ruby. i need one help in parsing text in ruby given @BreakingNews: Typhoon Morakot hits Taiwan, China evacuates thousands http://news.bnonews.com/u4z3 something like this i would like to eliminate all the hyperlinks. and get plain text. :@BreakingNews: Typhoon...

Calling parent module methods from a nested class

I'm having trouble figuring out how to call a method from a parent module in a class. I want to call module functions from parent module in my nested classes, but can't seem to find a way how to do this. example: module Awesome class Checker def awesome? awesome_detection end end module_function def awesome_dete...

Programmatically extract data from an Excel spreadsheet.

Is there a simple way, using some common Unix scripting language (Perl/Python/Ruby) or command line utility, to convert an Excel Spreadsheet file to CSV? Specifically, this one: http://www.econ.yale.edu/~shiller/data/ie_data.xls And specifically the third sheet of that spreadsheet (the first two being charts). ...

Refreshing a DIV after form submission in ruby on rails

I have a controller and a view (no ActiveRecord for this one). The controller calculates the current time in several places in the world and the view presents them. I'd like to add a button that will make the clocks update. Adding a regular button which refreshes the whole page works fine, but I'd like to do that asynchronously using AJ...

Using "should" with class methods?

I'm used to making calls such as: new_count.should eql(10) on variables, but how can I do something similar with a class method such as File.directory?(my_path)? Every combination of File.should be_directory(my_path) that I've tried leads to a method missing, as Ruby tries to find "be_directory" on my current object, rather than matc...