ruby

How to verify that "puts" has been called with a certain message?

Hello there. I'm trying to make this test fail :) it "should display the question" do @ui.should_receive(:puts).with("What's your name?").once @ui.ask_question("What's your name?") end At the moment it passes even if I don't call puts in my function. ...

ruby eval('\1') of gsub possible?

I try to replace a sub-str by the content of a valiable where its name matches the sub-str by: >> str = "Hello **name**" => "Hello **name**" >> name = "John" => "John" str.gsub(/\*\*(.*)\*\*/, eval('\1')) # => error! the last line in the code above is a syntax error. and: >> str.gsub(/\*\*(.*)\*\*/, '\1') => "Hello name" >> str.gsub...

ruby subclass filter

Hey! Maybe I am getting the idea of a subclass wrong, but I have a Person model and it has an attrib called "age" so Person.first.age #=> '20' Now I want to have a model that's basically persons 55 or older so I know I can have a class like this: class Senior < Person end But how can I "pre-filter" the Senior class so that every ob...

how do i get Ruby FileList to pick up files without a name, like .htaccess on windows

I want to search my filesystem for any files with the extension .template. The below works fine for everything except .htaccess.template FileList.new(File.join(root, '**', '*.template')).each do |file| # do stuff with file end because windows doesn't like nameless files, grrrr How do I make this work on Windows? This code works...

Ruby Oauth File upload/Multipart POST request

Hi I've been looking at this for a couple of days now and haven't found a solution. Is there a way to upload a file using OAuth-Ruby? I am working with a REST system that protects their resource with oauth. I am building a test tool using ruby and oauth-ruby to make it easier to upload test data to the system. But I can't get around to...

What's the point of some of shoulda's macros?

I think shoulda is really neat, but what I don't understand is why some of the macros exist, such as: should_validate_uniqueness_of :title should_validate_presence_of :body, :message => /wtf/ should_validate_presence_of :title should_validate_numericality_of :user_id I'm relatively new to testing, but what purpose do these serve? The...

If statement with code on same block in haml, RoR

Hello, i have this Haml view: !!! strict %html{:xmlns => "http://www.w3.org/1999/xhtml"} %head %meta{:content => "text/html; charset=utf-8", "http-equiv" => "Content-Type"} %title Lighty | #{@page_title || "Home"} %body #popup Login here #fade #wrapper #container #header ...

Multiple domains on a single rails app

This is the first time I'm creating an app using Ruby on Rails. I would like to switch the database depending on the site which is loaded. With php I used to do a simple strpos on http host, and set the database name depending on that. How can I do this with rails? ...

Rails Metaprogramming: How to add instance methods at runtime?

I'm defining my own AR class in Rails that will include dynamically created instance methods for user fields 0-9. The user fields are not stored in the db directly, they'll be serialized together since they'll be used infrequently. Is the following the best way to do this? Alternatives? Where should the start up code for adding the meth...

Ruby - Implicit object of a case statement

In Ruby, is there a way to get the implicit object of a case statement? case 2+2 when '2' puts '2' else puts "#{some_object}" end Where 'some_object' would be the return value of whatever statement was evaluated by case ...

How to sort a Ruby Hash by number value?

Hi everyone, I have a counter hash that I am trying to sort by count. The problem I am running into is that the default Hash.sort function sorts numbers like strings rather than by number size. i.e. Given Hash: metrics = {"sitea.com" => 745, "siteb.com" => 9, "sitec.com" => 10 } Running this code: metrics.sort {|a1,a2| a2[1]<=>a1[1...

Ruby Multithreading: making one thread wait for a signal from another

In Ruby, I want to have two threads running at the same time, and want the background thread to periodically signal the foreground thread. How do I get the foreground thread to block until the background thread says 'go'? I can think of a few ways to do it, but am after the most appropriate, idiomatic Ruby method. In code: loop do # b...

Rails ActiveRecord::MultiparameterAssignmentErrors

I have the following code in my model: attr_accessor :expiry_date validates_presence_of :expiry_date, :on => :create, :message => "can't be blank" and the following in my view: <%= date_select :account, :expiry_date, :discard_day => true, :start_year => Time.now.year, :end_year => Time.now.year + 15, :order => [:month, :year] ...

ruby convert hundredth seconds to timestamp optimization

Hey! I want to convert "123456" to "00:20:34.56" where the two digits to the right of the decimal point is in hundredth of a second. So 00:00:00.99 + 00:00:00.01 = 00:00:01.00 What I have: def to_hmsc(cent) h = (cent/360000).floor cent -= h*360000 m = (cent/6000).floor cent -= m*6000 s = (cent/100).floor cent -= s*100 "#{h...

Ruby on Rails: Model.all.each vs find_by_sql("SELECT * FROM model").each ?

I'm fairly new to RoR. In my controller, I'm iterating over every tuple in the database. For every table, for every column I used to call SomeOtherModel.find_by_sql("SELECT column FROM model").each {|x| #etc } which worked fine enough. When I later changed this to Model.all(:select => "column").each {|x| #etc } the loop starts out a...

Rails performance tests "rake test:benchmark" and "rake test:profile" give me errors

I'm trying to run a blank default performance test with Ruby 1.9 and Rails 2.3.5 and I just can't get it to work! What am I missing here??? rails testapp cd testapp script/generate scaffold User name:string rake db:migrate rake test:benchmark - /usr/local/bin/ruby19 -I"lib:test" "/usr/local/lib/ruby19/gems/1.9.1/gems/rake-0.8.7/lib/...

Rails STI: SuperClass Model Methods called from SubClass

I would like a little confirmation that I'm doing this correctly. Using rails single table inheritance I have the following models and class method: class PhoneNumber < ActiveRecord::Base def self.qual?(number) klass = self klass.exists?(:phone_number => phone_number) end end class Bubba < PhoneNumber end class Rufus < Pho...

Ruby sleep or delay less than a second?

So I'm making a script with ruby that must render frames at 24 frames per second, but I need to wait 1/24th of a second between sending the commands... how can I do that? sleep seems to only wait in increments of 1 second or more. update Well ya you can do sleep 0.1 if you want, but is this the best way to delay in a ruby script? ...

Why isn't `"repeat" * 3` the same as `3 * "repeat"` in Ruby?

When I type this: puts 'repeat' * 3 I get: >> repeat repeat repeat But it's not working if I do this: puts 3 * 'repeat' Why? ...

Ruby on Rails Report Generator

Hi There. Is there a way that I can produce a report in RoR without using Ruport or any other report generator rails plugin? Thanks in advance ...