ruby

What is the community-preferred Ruby unit testing framework?

In the Java space, JUnit is predominately used and in .NET I believe nUnit is very popular. Is a community agreed upon unit testing framework for the Ruby world? Background: I ask because I am new to Ruby and want to practice TDD at same time as I learn Ruby. So far I've only played with Test::Unit. ...

Which open-source git hosting software should I install on my company's intranet?

Instead of hosting my company's source code on Github, I would like to host it on our intranet under our control. What software could I use to simulate the same kinds of Git-based source-control features that are offered by sites like Github? Ideally this solution would be associated with a Ruby-based web application. ...

Ruby equivalent of PHP's ucfirst() function

What's the best way in Ruby (with Rails, if relevant) to capitalize the first letter of a string? Note that String#capitalize is not what I want since, in addition to capitalizing the first letter of the string, this function makes all other characters lowercase (which I don't want -- I'd like to leave them untouched): >> "a A".capital...

How to add convenience class methods to a Singleton class in ruby

Let's say I have a singleton class like this: class Settings include Singleton def timeout # lazy-load timeout from config file, or whatever end end Now if I want to know what timeout to use I need to write something like: Settings.instance.timeout but I'd rather shorten that to Settings.timeout One obvious way to ma...

How can I validate exits and aborts in RSpec?

I am trying to spec behaviors for command line arguments my script receives to ensure that all validation passes. Some of my command line arguments will result in abort or exit being invoked because the parameters supplied are missing or incorrect. I am trying something like this which isn't working: # something_spec.rb require 'somet...

Better alternative to try(:output).try(:data).try(:name)?

"output" is a serialized OpenStruct. def title   try(:output).try(:data).try(:title) end What would be better? :) ...

Troubles: Matrices, Vectors and Arrays

As far as I understood, matrices are very inflexible to work with. Therefor, I'm trying to get an array of vectors do deal with. My needs are: to be able to add vectors and make arithmetical operations on their components. Writing the code below, require 'matrix' x = Matrix.rows( IO.readlines("input.txt").each {|line| line.split} ) p...

Accessing an array of vectors trouble

My code looks like this: a = IO.readlines("input.txt").map { |line| Vector.[](line.split) } Now I want to access a single component of the first vector within my a array. I'm writing the following to address a vector: puts a[0] The behavior is pretty much expected - I receive the following: Vector[1.2357, 2.1742, -5.4834, -2.0735...

A bit complicated eval in Ruby

I need a special behavior from eval, to evaluate strings like: '5a + 6b + 3a + 11b' into '8a + 17b' Is it possible? If it is, can you give a proper example? I found this example recently, where the talk was about evaluating strings with meters and inches. ...

What's the advantage of making a Sinatra/Rack app into a single, locally-executable file?

I was reading the description of Vegas, which is a gem that aims to solve the simple problem of creating executable versions of Sinatra/Rack apps. I didn't know this was a problem that needed to be solved. The reason this seems of dubious benefit is because of this: Now if you run ./my_app it should: * find an appropria...

initialize all hashes with a default_proc

I wanted to extend the Hash class so that all hashes get same default_proc when they're created. So I put this in my file: class Hash def initialize self.default_proc = proc { |hash, key| raise NameError, "#{key} is not allowed" } end end This works fine if I use this syntax h = Hash.new but not if I use h = {} Playing wit...

Universal object cloning solution

Hi, I need a universal cloning solution to deep clone module with all it's instance variables in ruby. I can't really use .clone method, since it doesn't work on activerecord objects (doesn't copy the id field). I also saw a workaroung by using marshal dump + marshal load, but it doesn't work on module and singleton objects. Does anyone...

Isolating Ruby methods from overriding in subclasses? (like Java's private methods)

In Ruby, I'd like to acheive something like this Java sample: class A { private void f() { System.out.println("Hello world"); } public void g() { f(); } } class B extends A { public void f() { throw new RuntimeException("bad guy");} } public class Try { public static void main(String[] args) { new B().g();} } ...

What is the correct way to write a singleton pattern in Ruby?

I'm trying to write the most secure singleton in Ruby that I can. I'm new to the language, which is so elastic that I don't have a strong feeling that my singleton class will be successful at creating only one instance. As a bonus, I'd like the object to only become instantiated if really used. Thanks! ...

Is there an easy way to install RMagick?

I am trying to install RMagick on my slicehost(Linux Hardy) instead of compile from source, here is what I did: $ sudo aptitude install -y imagemagick $ sudo aptitude install -y libmagick9-dev $ sudo gem install rmagick After installed, it reads GIF with no problem, however for JPEG and PNG, I keep getting this error: Magick::ImageMa...

3d game engines for Ruby or Python ?

Are there any 3d game engines for these ? ...

After adding to an array in one method, the array appears empty in another

The following is outputting an empty unordered list. Why? Many thanks, Steven. module BrowserHelper def project_browser_helper if @project || @controller.controller_name == "projects" content_tag(:div, content_tag(:ul, build_browser), :class => "browser") end end def build_browser @browser = [] # a whole bu...

How can ruby provide a callback when a file is closed?

I have a method which returns an File object. I need to know when the file is getting closed. This is not as simple as overwriting the close method. The close method is not called if the file is closed by the destructor. It should work like this: def something get_lock_on_file file File.new("foobar") file.on_close { release...

Mechanize RuntimeError in Ruby 1.8.7

ruby mech.rb /usr/lib/ruby/gems/1.8/gems/mechanize-0.9.3/lib/www/mechanize/chain/uri_resolver.rb:53:in handle': need absolute URL (RuntimeError) from /usr/lib/ruby/gems/1.8/gems/mechanize-0.9.3/lib/www/mechanize/chain.rb:25:in handle' from /usr/lib/ruby/gems/1.8/gems/mechanize-0.9.3/lib/www/mechanize.rb:457:in fetch_page' f...

state design pattern from Java to Ruby

Hello, I have a working solution in java using a classic state design pattern and facing some difficulties translating it to ruby. I am new in ruby, but the diffuclty I believe lies in the differences on how patterns can be implemented in dynamic languages. My interface is describing the actions the model can execute in every state: p...