ruby

How to know if Spork is running

Hi. I'm trying to speed up "rspecing" in a rails 2.3.8 app with spork. When I run spork in the root of the project I get: (...stuff...) No server is running Running specs locally: Spork is ready and listening on 8989! And then, if I run specs, the message No server is running Running specs locally which appears if I run them w...

Ruby: kind_of? vs. instance_of? vs. is_a?

What is the difference? When should I use which? Why are there so many of them? ...

Ruby Curve Fitting (logarithmic regression) package

I am looking for a Ruby gem or library that does logarithmic regression (curve fitting to a logarithmic equation). I've tried statsample (http://ruby-statsample.rubyforge.org/), but it doesn't seem to have what I'm looking for. Anybody have any suggestions? ...

rspec user story feature vs cucumber

Just to be up front, although the title appears to be similar to the thread http://stackoverflow.com/questions/393622/rspec-vs-cucumber-rspec-stories, however my question is fundamentally different. I understand the difference between testing user stories and testing behavior of objects individually. My question is, why is Cucumber usua...

How do I stop Rails from misidentifying this class in a polymorphic :belongs_to?

I'm moving some code from being pulled in via SVN's externals function to be part of the code base proper. It's all Rails plugins and doing this seems to have broken part of my code. It seems like Rails is misinterpreting a :belongs_to statement. I have belongs_to :target :polymorphic => true in one of my classes and previously that has...

Creating a method in a class and using it in Ruby

Hi, so im trying to write a simple timer program in ruby. I defined my methods in a "Timer" class but when I call them it gives me a NoMethodError. Any ideas why? Thanks for the help. require "Time" class Timer def start $a = Time.now end def stop Time.now - $a end end puts "Type in 'Start'to to start the timer and then type 'S...

Ruby Unit test - Instance variable declared in setUp takes value nil

Hello I have a trouble with Ruby unit testing, I'm new to it so some help would be lovely class TestItem < Test::Unit::TestCase def setUp @item=Item.new('Food','Burger',120) end def testGetType assert_equal(@item.getType,'Food') end end Here the value of instance variable @item takes nil when I declare it in setUp() and use it ...

Is there a more efficient way to update the order of a collection of objects in Rails 3?

Suppose I have a collection of Pages that are ordered by a column called :sibling_order. Is there a more efficient way to update the ordering of the collection than what I'm doing below: class Page < ActiveRecord::Base ... def update_order(order) if (order < self.sibling_order) siblings = Page.where("parent_id = ? AND sibling_or...

In Ruby, what is the most correct way to use Regular Expression to match a single digit and **nothing else**?

(Update: this question's main focus is to test the "nothing else" part) Given a string s, which can contain anything, what is the most correct regexp in Ruby to check whether it is a single digit and nothing else? (a single digit and only a single digit). ...

What is the best way to ban/block users with Devise for Rails?

I'm using Devise for authentication in my rails app and I'd like to be able to block certain accounts and prevent users from reregistering with a blocked email. I'm just not sure what the best way is to go about it. My first thought was to override the sessions and registrations controllers to check the model for a user with a blocked ...

How to click a button element in watir?

Can I write watir code to click this button element? The onclick event has to be triggered. <button onclick="wizardAction('start', 'Starte Upgrade')"> <table align="center" cellspacing="0"> <tbody><tr> <td><img src="/static/images/stock/gtk-ok.png" align="left" height="16" width="16"></t...

In Ruby, how do you list/sort files before folders in a directory listing?

I have to following code in ruby: <% files = Dir.glob('/**/*') files.each do |file| puts file end %> It outputs (for example): /dirA/file1.txt /dirA/file2.txt /dirB/file1.txt /file1.txt /file2.txt /subdirA/file1.txt I want it to output it like this: /file1.txt /file2.txt /dirA/file1.txt /dirA/file2.txt /dirB/file1.txt /s...

Parsing HTML with Hpricot & Ruby - getting the innermost html?

I'm looking to parse some old html that has plenty of extraneous tags that could be done with CSS now - <b>, <font>, etc. I'm using Hpricot to parse it, but I want to get the innermost "inner_html" - how does one do that with Hpricot? For example lets say I user Hpricot to grab all the <table> elements which I loop through to get the r...

How to change this Ruby regex to also include underlines?

At the moment I am using this line to take a string and extract only the letters from it: string.scan(/[a-zA-Z]/).to_s How do I modify this so that the underline character, "_", is also included? Thanks for reading. ...

There has got to be a cleaner way to do this...

I have this code here and it works but there has to be a better way.....i need two arrays that look like this [ { "Vector Arena - Auckland Central, New Zealand" => { "2010-10-10" => [ "Enter Sandman", "Unforgiven", "And justice for all" ] } }, { "Brisbane Entertainment Centre - Brisbane Qld, Aus...

Rails 3 If Statement,,, IF XXX Equals At Least One (Var1, Var2, Var3)

Is the following statement possible with Rails/Ruby? If so, how :) IF XXX Equals At Least One (Var1, Var2, Var3) ...

'deadlock detected' error in rails

Hi, I have deadlock detected error in my code and don't understand why. Could someone please tell me what am I doing wrong? #!/usr/bin/ruby ENV['RAILS_ENV'] = ARGV.first || ENV['RAILS_ENV'] || 'development' require File.expand_path(File.dirname(__FILE__) + "/config/environment") mutex = Mutex.new threads = [] 1.upto(10) do |i| t...

When a module is imported into a Ruby class, is it similar to composition?

When you import a module into a class, is it similar in nature to OOP composition? ...

Is this possible? Edit new object accross more than one view without saving

Hello, I have a screen where a user can define new data records. There are several different fields in a data record where different images with various sizes can be added. To make it nice for the user to add all these images I'd like to separate the image upload portion to a greyboxed view. The layout flow would become Click new -...

Should a model method call 'save' itself?

Let's say that we have a method inside a model that needs to called only on saved records may update the model itself and thus the model needs to be saved again afterwords Should the "save" calls happen inside the method itself like the following code def result save! if new_record? # do some funky stuff here that may also cha...