ruby

Parsing a binary file in Ruby

I need to be able to parse a binary file with Ruby. This file contains chunks of data that are found via a header that includes the file offset and length of each chunk. How do I get the data out correctly? I've been unable so far to seek around in the file based on the offsets I read out since they come out in strings that I don't know...

Ruby: difference between @cars.each do |car| and for car in @cars do

(Sorry for the newbie question.) In Ruby, what is the difference between the loops: @cars.each do |car| and for car in @cars do ? is there a difference in efficiency, or why do we need two (or more) ways to express the same thing? The second way seems more elegant/natural to me, but I may be missing some crucial observation, why t...

Rails Nested Dynamic Objects?

I have a model that has lots of instances of a child model (a widget has many parts). Each part has a label and a value. What I am trying to achieve is when creating a new widget, I get a form listing all the potential parts and a field where I can enter the value. At the moment the list of potential parts is in an array in the Widget...

What is the recommended way of vendoring rails for a production application?

Rather than using the latest Rails gem for my application, I like to have the code local in my own git repository, which means putting it in vendor/rails. There are a couple of ways of doing this: downloading the source for the particular branch/tag I want to run and committing it to my repository, or using git submodules. Submodule...

Is there native support in Rails or Ruby for representing threaded comments

I want to have a comments section in my app that looks like this: response1 response1a response1b response1b1 response2 response2a response2b response2c response2c1 response2c1a response2c1a1 response2c1a1 response2c1a1a response2c1a1a1 Assuming I do this by using HTML such as the following: <div cla...

Ruby: how do I calculate a duration given 2 datetimes from a CSV file?

I have a number of CSV files which contain start and finish times. I can use FasterCSV to parse the files, but I don't know the best way to get the duration in Ruby. Here are the values in my hash generated for each row of the CSV files. start time: Mon Jul 20 18:25:17 -0400 2009 end time: Mon Jul 20 18:49:43 -0400 2009 ...

Rails Trouble creating model instance with one to many relationship

I think there are a lot of places where my design may be screwing this up. I have very limited experience with Rails though. This is happening in Rails 2.3.2 with Postgres 8.3. We've got two tables in our DB. One called "survey" and one called "survey_timepoint". A survey can have multiple time points so in the survey_timepoint tabl...

HTTPS and HTTParty - Timeout and EOF

Hi all, I'm trying to post something to an HTTPS resource, but it seems it doesn't work. My code look something like this: require 'httparty' class MyClass include HTTParty base_uri "https://mydomain.com:8085/search" basic_auth 'admin', 'changeme' format :xml def mymethod self.class.post('/job', :query => ...

Using ruby and nokogiri to parsing HTML using HTML comments as markers

How could I use ruby to extract information from a table consisting of these rows? Is it possible to detect the comments using nokogiri?   EXTRACT LINK 1 EXTRACT DESCRIPTION EXTRACT LINK 2 Mr P 1 ...

How to automate a keystroke using Win32 and Ruby?

I wonder if anyone knows how to use Win32 to automate a keystroke using Ruby? ...

(Ruby) Converting string values into assignable properties for OpenStructs...?

Hi All, I've got a bit of an odd situation. If I were using a hash, this issue would be easy, however, I'm trying to use "OpenStruct" in Ruby as it provides some decently cool features. Basically, I think I need to "constantize" a return value. I've got a regular expression: textopts = OpenStruct.new() textopts.recipients = [] ...

So maybe I'm not getting the idea in Ruby but I have a question about Enumerables inject.

The |m,k| thing kind of throws me off. Does this have anything to do with order of precedence? m standing for 0 (or 1 in some languages) and k for the last in the Array/Hash whatever? So why do people put a number in .inject()? Alternatively, is there an easy way to learn how to use this, and exactly what it's value is? Judging from th...

Overriding/Modifying Rails Class (ActiveResource)

I've been struggling with an issue with ActiveResource for a bit now: when a hostname resolves for an ActiveResource request, but there's no server on the other end to return information, ActiveResource's timeout value doesn't work. The request just hangs. After reviewing the ActiveResource code, I've realized that this is because the u...

Model-specific SQL logging in rails

In my rails application, I have a background process runner, model name Worker, that checks for new tasks to run every 10 seconds. This check generates two SQL queries each time - one to look for new jobs, one to delete old completed ones. The problem with this - the main log file gets spammed for each of those queries. Can I direct t...

Send allows access to private variables

Hello! Consider the following code: def create_class(class_name, superclass, &block) klass = Class.new superclass, &block Object.const_set class_name, klass end After I do: create_class('User', ActiveRecord::Base) the following is ok: Object.send(:remove_const, :User) but this: Object.remove_const :User results in th...

Ruby: Struct vs OpenStruct

In general, what are the advantages and disadvantages of using an OpenStruct as compared to a Struct? What type of general use cases would fit each of these? ...

nginx rewrite rules with Passenger

Hi, I'm trying to migrate to nginx from Apache using Passenger in both instances to host a Rails app. The app takes a request, which is for an image- if the image exists at /system/logos/$requestedimage then it should get served, or it should be allowed to hit the Rails app to generate it if needed (where it is then cached to /system/lo...

Where to place Rails code that is not a model, view, controller or helper?

I want to share code not related to views between several controllers in my Rails app. Where in the directory structure should I place it? EDIT: the code in question if something all controllers use to determine how they render the model data ...

Feature envy smell in my code, pls explain?

Eclipse (RedRails) complain about "Feature envy" in the following code: if input_text =~ /^(---\s*\n.*?\n?)(---.*?)/m content_text = input_text[($1.size + $2.size)..-1] # warning in $1 header = YAML.load($1) @content = content_text.strip() @title = header["title"] end My understanding is that I safe to ignore this warning. B...

How do I test a file upload in rails?

I have a controller which is responsible for accepting JSON files and then processing the JSON files to do some user maintenance for our application. In user testing the file upload and processing works, but of course I would like to automate the process of testing the user maintenance in our testing. How can I upload a file to a control...