ruby

Establishing a has_many relationship in Rails

I'm having trouble creating an application in Rails. This application has two models, one to represent companies and the other to represent products. Each company sells zero or more products and I'm looking to capture this relationship in the model. Here's what I've got so far: I created the models by running the generator: script/gen...

Cucumber::Rails::World NoMethodError for IntegrationTest methods

I have a set of Cucumber features that are successfully running on a Mac OS X 10.5 machine and a Windows XP machine. I tried running them on a Mac OS X 10.4 machine today and Cucumber can't find any of the ActionController::IntegrationTest methods on the Cucumber::Rails::World. I am receiving the following error when trying to send a ge...

How do I match valid words with a ruby regular expression.

Using a ruby regular expression, how do I match all words in a coma separated list, but only match if the entire word contains valid word characters (i.e.: letter number or underscore). For instance, given the string: "see, jane, run, r#un, j@ne, r!n" I would like to match the words 'see', 'jane' and 'run', but not the word...

regexp split string by commas and spaces, but ignore hyphenated words?

I need a regexp to split a string by commas and/or spaces, but ignore hyphenated words -- what's the best way to do this? so, for example -- I'd like this ... "foo bar, zap-foo, baz".split(/[\s]+/) to return ["foo", "bar", "zap-foo", "baz"] but when I do that it includes the commas like this ... ["foo", "bar,", "zap-foo,", "baz"...

Array to hash of key value pairs in ruby

From a model that returns all the values from a table, how would I convert that to a hash of name value pairs {column_value => column_value} e.g. [{:id => 1, :name => 'first'}, {:id => 2, :name => 'second'}, {:id => 3, :name => 'third'}] to (specifying :id and :name) {'first' => 1, 'second' => 2, 'third' => 3} ...

Conditional Dependency in Ruby Gemspec

I'm building a gem that needs a json gem installed in order for it to work. It doesn't matter which json gem: json_pure, json-jruby, or the C-based json. Is there a good way of defining this in a gemspec? This response suggests maintaining a completely separate gem for each version, but it seems like there has got to be a better way. ...

Normalizing line endings in Ruby

I have a string in Ruby, s (say) which might have any of the standard line endings (\n, \r\n, \r). I want to convert all of those to \ns. What's the best way? This seems like a super-common problem, but there's not much documentation about it. Obviously there are easy crude solutions, but is there anything built in to handle this? Ele...

Is there a performance gain in using single quotes vs double quotes in ruby?

Do you know if using double quotes instead of single quotes in ruby decreases performance in any meaningful way in ruby 1.8 and 1.9. so if I type question = 'my question' is it faster than question = "my question" I imagine that ruby tries to figure out if something needs to be evaluated when it encounters double quotes and prob...

Freezing database with Rails application

So for a class I have to turn in my Rails application to my professor. What is the best way to make sure everything goes smoothly when he trys to start it up? Also, is there anyway I can freeze a database and send that with it so he has all of the data I have been using in the application? Thanks a lot. ...

use strategy pattern for billing models that use different data for the calculation?

we have an invoice model that bills clients in a few different ways. for brevity sake, i'm going to focus on two: cost per impression and cost per phone inquiry. my thought was to implement these (and the rest) as strategies and then dynamically mix them in to the invoice class. this seems appropriate because there are different source...

Special characters with Ruby XML Builder

I'm trying to a Google kml tour with ruby and I get a syntax error with this code xml = builder.gx:Tour It doesn't like the colon. Is there a way to force it to compile this? ...

What's the best AIR & Ruby integration tool/framework?

Well, I tried Shoes, Titanium, and RubyFX (or was it FXRuby?) and am not yet quite happy with the stability and cross-platform support from any of them as desktop application GUI tools. Next in line is Adobe AIR. Anyone know what the best tool is that will integrate Ruby and Adobe AIR? Is it even possible? ...

Ruby Equivalent of C# 'using' Statement

I've been getting into Ruby over the past few months, but one thing that I haven't figured out yet is what the Ruby equivalent of C#'s (and other languages) using statement is. I have been using the require statement to declare my dependencies on Gems, but I am getting lazy and would prefer to not fully qualify my frequently used class ...

Obscure your Ruby code?

I am in the middle of developing an enterprise application using RoR (first time for us to build an enterprise app on RoR instead of Java), and while we do not have that much problem of obscuring the source code, I was still wondering if this was possible. Whether we could somehow just have a simple EXE or something else, such that our c...

How to build a web-based chat system using ruby Gserver

I am trying to build a web based chat system and I am going to user ruby gserver. I have looked at this example . However my question is when I get the user input from the web and in the controller I have the user input. Now how does client connect to server to pass this user input value to the server. The server after getting the valu...

Accessing a resource in routes.rb by using attributes other than Id

I have the following in my routes.rb map.resources :novels do |novel| novel.resources :chapters end With the above defined route, I can access the chapters by using xxxxx.com/novels/:id/chapters/:id. But this is not what I want, the Chapter model has another field called number (which corresponds to chapter number). I want to access...

How can I get nokogiri to select node attributes and add them to other nodes?

Is it possible to grab a following element's attributes and use them in the preceding one like this?: <title>Section X</title> <paragraph number="1">Stuff</paragraph> <title>Section Y</title> <paragraph number="2">Stuff</paragraph> into: <title id="ID1">1. Section X</title> <paragraph number="1">Stuff</paragraph> <title id="ID2">2. S...

reading a pdf, adding text/images to it and write the modified pdf in rails

I want to use PDF::writer to put some dates on a calendar sheet. The calendar sheet itself is prepared as a prerendered template in PDF format. How could I read this PDF as template to write text on it? Is there an alternative? Fiddling with an HTML-to-PDF converter (like HTMLDoc) is no option. ...

Problem with has_many :through and fields_for.

Hi There, I have a pretty basic association: # user.rb class User < ActiveRecord::Base has_many :services, :through => :subscriptions has_many :subscriptions, :accessible => true accepts_nested_attributes_for :subscriptions end # service.rb class Service < ActiveRecord::Base has_many :users, :through => :subscriptions has_ma...

Ruby - Operator precedence ? And/&&

Hi, I have a question regarding the and/&&/= keywords in Ruby. The ruby docs say that the precedence for the mentioned keywords is: (1)&&, (2)=, (3)and. I have this snippet of code I wrote: def f(n) n end if a = f(2) and b = f(4) then puts "1) #{a} #{b}" end if a = f(2) && b = f(4) then puts "2) #{a} #{b}" end...