ruby

in Ruby, trying to convert those weird quotes into "regular" quotes

I am trying to parse a text file that has the weird quotes like “ and ” into "normal quotes like " I tried this: text.gsub!("“",'"') text.gsub!("”",'"') but when it's done, they are still there and show up as \x93 and \x94 so I tried adding that too with no luck: text.gsub!('\\x93', '"') text.gsub!('\\x94', '"') The problem is,...

How to create heroku based Sinatra apps

I am trying to create Sinatra based heroku app without any luck? ...

Disadvantages of sequel gem

Are there any alternatives to sequel orm when working on Sinatra based app ...

Passing output of named_scope to another method

I am retrieving data on individual lines within a report with the following named_scope in my survey_response.rb model: named_scope :job_responses, lambda{|job_code, survey_code| {:conditions => ["survey_job_id = ? AND survey_id = ?", job_code, survey_code]}} Checking the log I can see that my SQL query is executed as expected. The q...

How do I create a helper with block?

...

Design for Ruby app using meta programming

I am making a framework where objects should be created a according to a predefined XML file. For example, if in the xml file the following occurs: <type name="man"> <property name="name" type="string"> <property name="height" type="int"> <property name="age" type="int"> <property name="profession" type="string" value="...

How do I change the zone offset for a time in ruby?

I have a variable foo that contains a time, lets say 4pm today, but the zone offset is wrong, i.e. it is in the wrong time zone. How do I change the time zone? When I print it I get Fri Jun 26 07:00:00 UTC 2009 So there is no offset, and I would like to set the offset to -4 or Eastern Standard Time. I would expect to be able to just...

How do I collect and combine multiple arrays for calculation?

I am collecting the values for a specific column from a named_scope as follows: a = survey_job.survey_responses.collect(&:base_pay) This gives me a numeric array for example (1,2,3,4,5). I can then pass this array into various functions I have created to retrieve the mean, median, standard deviation of the number set. This all works...

Send email from Ruby program with TLS

I am trying to send an email from a Ruby program. The smtp server is an Exchange 2007 server that requires me to login to send the email. #!/usr/bin/ruby require 'rubygems' require 'net/smtp' msgstr = "Test email." smtp = Net::SMTP.start('smtp.server.com', 587, 'mail.server.com', 'username', 'password', :login) do |smtp| smtp...

What are alternatives to find_by_sql for computationaly-heavy queries?

Our company loves reports that calculate obscure metrics--metrics that cannot be calculated with ActiveRecord's finders (except find_by_sql) and where ruport's ruby-based capabilities are just too slow. Is there a plugin or gem or db adapter out there that will do large calculations in the database layer? What's your solution to creati...

How to run Rails console in the test environment and load test_helper.rb?

The background: I'm having some problems with Thoughtbot's "Factory Girl" gem, with is used to create objects to use in unit and other tests. I'd like to go to the console and run different Factory Girl calls to check out what's happening. For example, I'd like to go in there are do... >> Factory(:user).inspect I know that you can run...

action caching and queued posts in rails

I have a landing page on rails with "blog" style posts. Since it's the most visited page on the site, I'm using action caching. The cache is cleared upon a change of the model. However, when adding new blog posts, i'd like to be able to create posts in the --future-- (a la queueing posts in Tumblr). So simply observing the model won't...

Rails model that joins through a join table

Is this possible? In an events system an event can have multiple times. (ie, if it is a 3 day event and each day it's at a different time). Each time has a place associated with it. Finally, each place has an address associated with it. Now, can I reference those addresses through my event model? I'm thinking something conceptually lik...

Ruby module with a static method call from includer class

I need to define the constant in the module that use the method from the class that includes this module: module B def self.included(base) class << base CONST = self.find end end end class A def self.find "AAA" end include B end puts A::CONST But the compiler gives the error on the 4th line. Is there any ...

Can a Ruby method yield as an iterator or return an array depending on context?

I have an arbitrary method in Ruby that yields multiple values so it can be handed to a block: def arbitrary yield 1 yield 2 yield 3 yield 4 end arbitrary { |x| puts x } I'd like to modify this method so that, if there is no block, it just returns the values as an array. So this construct would work as well: myarray = arbitr...

IT guy picking up programming - C#, Python, or Ruby

Hi There (this turned out kinda long but i appreciate you reading), I've been in IT for about 10 yrs. I've had exposure in everything from rolling out server environments, to routing, to security. I've worked quite a bit with web servers and have been exposed to the superficial structure of things like asp.net, css, html, xml, php, my...

Including a Ruby class from a separate file

For a while I had been including an entire class inside of a Ruby module. Apparently this is not what I am supposed to do. It appears that the point of a module is to store functions which can then be included as methods in a new class. I don't want this. I have a class that I want to keep in a separate file which I can access from oth...

Rails assert that form is valid

What's the best practices way to test that a model is valid in rails? For example, if I have a User model that validates the uniqueness of an email_address property, how do I check that posting the form returned an error (or better yet, specifically returned an error for that field). I feel like this should be something obvious, but as...

Rails can't find the generator class

I have some code that is using SyncEnumerator. As you can see here, SyncEnumerator requires generator.rb. To include this, the developer of this plugin has require 'generator'. This worked on my previous Rails installation but is not working since I upgraded OS X and reinstalled Rails. Now when I try to install my gems, I get: Macint...

Using a duration field in a Rails model

I'm looking for the best way to use a duration field in a Rails model. I would like the format to be HH:MM:SS (ex: 01:30:23). The database in use is sqlite locally and Postgres in production. I would also like to work with this field so I can take a look at all of the objects in the field and come up with the total time of all objec...