ruby

Communicating between a ruby script and a running c++ program

I have a c++ program which performs one function. It loads a large data-file into an array, receives an array of integers and performs a lookup in that array, returning a single integer. I am currently calling the program with each integer as an argument, like so: $ ./myprogram 1 2 3 4 5 6 7 I also have a ruby script, and I would like...

paperclip polymorphic association and saving

I changed my photo.rb model to be polymorphic and be usable to all sorts of other models needing to save images and it works fine except I can't figure out how to save new attachments properly through the parent model. Any ideas? Do I have to approach this differently somehow? As, its also not getting the imageable_type...which i'll h...

Avoiding/Removing nil when using a case statement while parsing a string

sample data: DNA : This is a string BaseQuality : 4 4 4 4 4 4 6 7 7 7 Metadata : Is_read DNA : yet another string BaseQuality : 4 4 4 4 7 7 4 8 4 4 4 4 4 Metadata : Is_read SCF_File . . . I have a method that is using a case statement as follows to separate parts of a longer text file into records using the delimeter "\n\n"...

Sass Compass multiple themes ruby on rails

hi all I have a rails app that is a sort of CMS and i alow users to create their own version of the applicaton on a sub domain. I also want to let them style their app version the way they want but changing a few fields or small config file. I was hoping to be able to use sass and compass and be able to let users change varialbes for ...

how to hide a div in javascript with onblur - rails

i have a link_to_function that shows a hidden div. now i would like to hide this div if the user clicks out of this div(onBlur or onclick). when should i call this function and how? this is my function that shows the hidden div: <%= link_to_function "ShowHorse", "$('horsePic').show();" :class =>"links_02"%> shoud it be from inside thi...

How to time an operation in milliseconds in Ruby?

Hello, I'm wishing to figure out how many milliseconds a particular function uses. So I looked high and low, but could not find a way to get the time in Ruby with millisecond precision. How do you do this? In most programming languages its just something like start=now.milliseconds myfunction() end=now.milliseconds time=end-start ...

Automate conversion of Sybase .ADT files to SQL.

I am working with some data I obtained that is read with a program using an embedded Advantage Database Server. The program was not written by me and does not have all of the functionality that I need. I would like to convert this data to a different format so that I can work with it more freely, such as MySQL. I know that Sybase pr...

Closure doesn't work

If block is a closure, why this code does not work? And how to make it work? def R(arg) Class.new do def foo puts arg end end end class A < R("Hello!") end A.new.foo #throws undefined local variable or method `arg' for #<A:0x2840538> ...

Where should I create a shortened URL to my users' profile page for MVC architecture?

I have a Rails app where people have a profile page - http://prettylongdomainname.com/profile_username To create the profile username, I use a before_create AR hook in my model: before_create :generate_username def generate_username self.username = a_user_name_i_generated end I would also like to save a shortened URL to the user...

Is an object in a given collection?

Let's say I have a blog application. On each Post there are Comments. So, I have a collection of comments in "Post.comments" Let's say I have an object called "this_comment" Is there a magical keyword or construct in Ruby such that I can test "is 'this_comment' in 'Post.comments"? In other words, I want to know if "this_comment" is a...

In Ruby why won't `foo = true unless defined?(foo)` make the assignment?

What's going on here? What is the subtle difference between the two forms of "unless"? > irb(main):001:0> foo = true unless defined?(foo) => nil irb(main):002:0> unless defined?(fooo) ; fooo = false ; end => false thx ...

ruby externalized SQL module

Any recommendations for a module which keeps SQL queries external to the application, for Ruby programs? I'm looking to avoid hardcoding SQL queries and possibly to support multiple SQL backends in a set of programs that make direct SQL queries (that is, not mediated via an ORM). Hyopthetically: # Production system is pg, dev environm...

While loading webpage: "undefined method `request_uri' for #"

I'm trying to use Ruby to load a webpage over HTTP and check what its status code is. My code looks like this: require "net/http" @r = Net::HTTP.get_response(URI.parse(myURL)) return @r.code However, for some URLs (mostly ones pointing to weird stuff like web counters that won't give a proper response) I'm getting an undefined m...

Is there a fast and reliable way of serializing objects across different versions of Ruby?

I have two applications talking to each other using a queue, as of now they run exactly the same version of ruby (1.8.7), so I'm just marshaling objects back and forth; only objects from the standard lib mostly hashes, strings, time and date objects. Right now I'm moving to Ruby 1.9.1, one app at the time, which means I'll be running o...

link_to_function with rails 3

Hi, link_to_function raise an error with rails 3. undefined method `link_to_function' for #<Class> How can i do to have a link with a simple onclick, with the new rails 3 syntax? Thanks ...

how to access array[3..last] (ruby)

how can I access all array elements from x to the last one? my_array= [1,2,3,4,5,6] puts my_array[3..last] ...

before_filter not working in state_machine :(

Hi all, I have a model that relies on state_machine to manage its different states. One particular event requires a before_transition as it needs to build a join table fore doing the transition. Unfortunately it doesn't work. class DocumentSet < ActiveRecord::Base state_machine :state, :initial => :draft do # Callbacks befor...

save an active records array

I have an array like this a = [] a << B.new(:name => "c") a << B.new(:name => "s") a << B.new(:name => "e") a << B.new(:name => "t") How i can save it at once? ...

Ruby doesn't recognize the g flag for regex...

Is it implied by default in str.scan? Is it off by default in str[regex] ? ...

Using Rails form helpers on non-existent methods

Normally, when using form helpers in Rails, each field directly correlates to a method on the appropriate object. However, I have a form (user signup) that needs to include fields that are not part of the user model itself (for instance, card details) but need to appear. How can I build the form so that I can get the necessary fields, ...