ruby

ruby adding variable value to an array

I have a Ruby 1.8.6 script that is supposed to take a filename that contains a path, create the folders then move the file into the folders. I have an array created_paths to keep tracked of folders created (the script will loop through lots of files). I am having a problem adding to the created_paths array. created_paths = Array.new f...

Is there a Ruby CSV parser that generates column getter methods?

Hi there, I'm trying to generalize a data retrieval mechanism with Ruby, and can't seem to find a way to retrieve a CSV file and access one of the row's columns by using a dot operator like so: Let's say I have a CSV table: #some_file.csv name,age albert,13 And I create a FasterCSV table from it: a = FasterCSV.new(File.open('some_fi...

How can I "replace" a module included via ruby include function

As a follow up to http://stackoverflow.com/questions/2403057/how-can-i-reverse-rubys-include-function, which was well answered but turned out my simplification of the real problem mean't that the solution was not applicable. I'm now faced with this (names changed to protect identities!): module OldFormHelpers def foo puts "foo" ...

Pubsubhubbub on Rails. How to extract the raw POST body contents from the POST request?

I am having trouble setting up a pubsub enabled subscriber app using rails. I have currently subscribed to the open hub pubsubhubbub.appspot.com and am receiving pings to my application's endpoint. (as of now i have created a counter which increments everytime the end point is pinged). But i am not able to understand as to how to extract...

Run sql script from Ruby

Hi all, Using DBI::DatabaseHandle#execute or DBI::DatabaseHandle#prepare it's not possible to run an sql script (with mutiple sql statments). It fails with the following error : ERROR: cannot insert multiple commands into a prepared statement I tried to use the "unprepared" way using DBI::DatabaseHandle#do (the doc says it "goes stra...

Rails belongs_to association, can't access owner's attributes when part of a collection?

I have an Object, Ball, which belongs_to a Girl, which can have_many balls. Everything works for the most part, but if I try to print out the girls' name via: @balls.each do |b| b.girl.name end I get the following error: "undefined method `name' for nil:NilClass" Which really confuses me. If I say b.girl.class, I get it as an i...

Find a duplicate entry in a large SQL script

Hello I have a very large SQL script and while trying to execute it I get an error from PostgreSQL about a duplicate primary key. It does not give a line number of anything for where the duplicate entry occurs (it is also wrapped in a transaction, so it doesn't know about the duplicate until commit; at the end of the file. Basically, co...

Treetop grammar issues using regular expressions

I have a simple grammar setup like so: grammar Test rule line (adjective / not_adjective)* { def content elements.map{|e| e.content } end } end rule adjective ("good" / "bad" / "excellent") { def content [:adjective, text_value] end } e...

Confused as to which Prototype helper to use (updated)

This is a continuation of http://stackoverflow.com/questions/2397874/confused-as-to-which-prototype-helper-to-use. My code has been updated to reflect other user's suggestions: (model) message.rb: class Message < ActiveRecord::Base after_create :destroy_old_messages def old_messages messages = Message.all(:order => 'updated_at ...

Getting information from scripts/command line calls in C#

I've been writing a couple of apps which use C# as the Gui, but under the hood do all the work via scripts (which may be Python, Ruby etc.). To pass information from the script back to the GUI (for example error reporting etc.) I've usually resorted to calling the script via Process and either Redirected the input (StartInfo.Redirec...

how to make a variable seen in all views - rails

so i have many controllers and many views. i want my variable @random_quote to be evaluated each time every view loads up. i tried several things in application controller(i thought it should be here?) but none of them worked. so how do i connect these two: @random_quote.body (in view) and @random_quote = Quote.find(:random) (in c...

How to monkey patch a rake task shipped with Rails?

I just found a bug in one of the rake tasks shipped with Rails. Is there a way to monkey patch a rake task? ...

Ruby TCPServer sockets

Maybe I've gotten my sockets programming way mixed up, but shouldn't something like this work? srv = TCPServer.open(3333) client = srv.accept data = "" while (tmp = client.recv(10)) data += tmp end I've tried pretty much every other method of "getting" data from the client TCPSocket, but all of them hang and never break out of th...

Properly handling unicode characters in Rails

By default Rails allows users of our application to input non-utf8 data, such as: ¶®«¼ However when we attempt to retrieve the data from our database and render it in a template Rails incorrectly assumes that it is in UTF-8 format and throws an error. ArgumentError: invalid byte sequence in UTF-8 What is the best way to handle this? ...

Would it be possible to integrate Python or Perl with Ruby?

Would it be possible to integrate Python (and/or Perl) and Ruby? I've looked at http://www.goto.info.waseda.ac.jp/~fukusima/ruby/python/doc/ and http://code.google.com/p/ruby-perl/ , but they both seem rather outdated. Has someone generated a Ruby interface for Python's C API? Edit: Python can be integrated with many other languages ac...

Why is Phusion Passenger refusing to recognize my updated RubyGems?

I've updated RubyGems everywhere I can possibly think to update it: but Phusion passenger keeps throwing the same error: There is that one freaking place that shows 1.3.1 as the version, despite the file name of 1.3.6. I've obviously borked things up pretty badly here. That is, I need to update to 1.3.2 or greater and that I curr...

Can't install any gems...

Ok here is the background, I have been doing JavaScript and some Erlang for around 6 months and I haven't done any rails programming lately. Today on my new PC I went to install rails but got this error: gem install rails WARNING: RubyGems 1.2+ index not found for: http://gems.rubyforge.org/ RubyGems will revert to legacy inde...

Understanding RubyGems, Macports, and /opt/ versus /Library/

I'm not looking to keyword spam here, and this question is in the least tangential to a previous question of mine that's currently pending. Caveat emptor. Most references to Ruby on the Mac have things set up with the conventions mentioned in this question. Stored in /Library/, that is. My whole setup is somehow stored like this: I'...

Ruby: call list of methods until one returns true

Is there a pretty way to make a series of method calls in ruby UNTIL one returns true? This was my first thought, but was thinking there might be a nicer way: if method_one elsif method_two elsif method_three else puts "none worked" end ...

Rails autosave association in controller action

I have the following one to many associations. Document has many Sections and Section has many Items. class Document < ActiveRecord::Base has_many :document_sections, :dependent => :destroy, :autosave => true has_many :document_items, :through => :document_sections end class DocumentSection < ActiveRecord::Base belongs_to :docume...