ruby

How to compare Floats with delta?

Is there in Ruby some functionality/syntax to compare two floats with delta? Something similar to *assert_in_delta(expected_float, actual_float, delta)* from test/unit but returning Boolean? ...

How to embed Ruby in C++?

What's the best way to embed Ruby as a scripting language in C++? Using ruby.h? SWIG? Something else? What I need is to expose some C++ objects to Ruby and have the Ruby interpreter evaluate scripts that access these objects. I don't care about extending Ruby or accessing it in C++. I've found this article on embedding Ruby in C++, and ...

For ruby/webrick, I need windows to recognize shebang (#!) notation.

(Bear with me, I promise this gets to shebang and windows.) I have about the simplest of WEBRick servers put together: require 'webrick' include WEBrick s = HTTPServer.new(:Port=>2000, :DocumentRoot=>Dir::pwd) s.start Couldn't be simpler. This basic server does accept http connections (firefox, internet exploder, wget, TELENT) and ...

How do I detect how to escape spaces in a path

In Cygwin a space in a path has to be escaped with a backslash Not true in Windows, put the whole path in a quote Is there a way to convert to this automatically in Ruby? Otherwise, how in Ruby do I detect if I am running with Windows or Cygwin? ...

Sieve of Eratosthenes in Ruby

Rather than scraping a Ruby version of this algorithm off the net I wanted to create my own based on its description here. However I cannot figure out two things def primeSieve(n) primes = Array.new for i in 0..n-2 primes[i] = i+2 end index = 0 while Math.sqrt(primes.last).ceil > primes[index] (primes[index] ** 2).ste...

Why don't Searchgasm page links work?

I'm using the Searchgasm plugin for my searching and pagination. Everything seems to work great but the page links don't seem to work at all. Has anyone had this problem with Searchgasm before? Controller Code: class ArtistsController < ApplicationController # GET /artists # GET /artists.xml def index @search = Artist.new_s...

Why does Ruby seem to have fewer projects than other programming languages?

I've found Ruby to be very attractive; I like the fact that everything is an object and its syntax is very appealing. I was hoping that it would gain a lot of popularity this year, but I don't see lot of activity in Ruby. For instance if we take the number of tags added in SO there are only about 700 questions tagged as "ruby." This ...

How do I make a POST request with open-uri?

Is it possible to make a POST request from Ruby with open-uri? ...

How to increase stack size for a ruby app. Recursive app getting: Stack level too deep (SystemStackError)

Posting a stack overflow question on stackoverflow.com, how amusing :-) I'm running some recursive Ruby code and I get the: "Stack level too deep (SystemStackError)" (I'm quite sure the code works, that I'm not in an infinite recursive death spiral, but that is not the point anyway) Is there anyway to change the allowed stack depth/si...

How can I see the SQL ActiveRecord generates?

I'd like to check a few queries generated by ActiveRecord, but I don't need to actually run them. Is there a way to get at the query before it returns its result? ...

create object from xml string in ruby

I am trying to deserialize an object from xml in ruby. Something simple like u = User.new({:client_key => "Bar"}) v = User.new(u.to_xml) I get an error NoMethodError: undefined method `stringify_keys!' for #String:0x20fc7cc>. I'm not sure what I have to do in order to get the string from xml to an object. Update: @avdi gave me the...

Adding to multiple tables in rails

I'm sure this is a relatively simple question, and there must be a good sensible rails way of doing it, but I'm not sure what it is. Basically I'm adding books to a database, and I want to store the Author in a separate table. So I have a table called authors which is referenced by the table books. I want to create a rails form for add...

Sharing code in respond_to blocks

I have the following before_filter: def find_current_membership respond_to do |wants| wants.html { @current_membership = @group.memberships.for(@current_user) } wants.rss {} wants.js { @current_membership = @group.memberships.for(@current_user) } end end I would like to share the code for the HTML and ...

text_field_with_auto_complete inside form_for

Simple question really - how do I use text_field_with_auto_complete inside a form_for block? I've tried doing f.text_field_with_auto_complete but that gives an error, and just using text_field_with_auto_complete by itself doesn't seem to do anything. Am I missing something here? ...

What are the Conventional GEM PATHS for Ruby under OS X 10.5?

I have a performance problem with my ruby on my machine, which I think I have isolated to loading libraries (when #require is called), so I'm trying to work out whether ruby is searching too many folders for libraries. When I run $ gem environment RubyGems Environment: - RUBYGEMS VERSION: 1.3.0 - RUBY VERSION: 1.8.6 (2008-03-03 pa...

Can't Activate Rake (> 0.0.0) ??

Ok, this is very weird. I'm trying to do a database migration, and all of a sudden, I'm getting these errors: [C:\source\fe]: rake db:migrate --trace (in C:/source/fe) ** Invoke db:migrate (first_time) ** Invoke setup (first_time) ** Invoke gems:install (first_time) ** Invoke gems:set_gem_status (first_time) ** Execute gems:set_gem_sta...

What is the equivalent of .NET events in Ruby?

The problem is very simple. An object needs to notify some events that might be of interest to observers. When I sat to validate a design that I cooked up now in Ruby just to validate it.. I find myself stumped as to how to implement the object events. In .Net this would be a one-liner.. .Net also does handler method signature verificat...

How do you generate rDoc for a particular plugin using rake

OK, I am trying to generate the rDoc for paperclip, but the rake task is choking on another plugin before it gets to it, so this is out of the question: rake doc:plugins I could go and fix up the broken plugin, but I'm busy and lazy, so I just want to generate the docs for paperclip. Is there any way of doing this? ...

How do I specify the "commentable_type" field with polymorphic associations?

I have two models, Article and Post that both inherit from a base model called ContentBase. You can leave comments on both Articles and Posts, so I am using a Polymorphic Association between Comments and Article or Post. However, since both Article and Post inherit from ContentBase, the commentable_type field ends up being "ContentBase...

Looking for suggestions for building a secure REST API within Ruby on Rails

I'm getting started on building a REST API for a project I'm working on, and it led me to do a little research as to the best way to build an API using RoR. I find out pretty quickly that by default, models are open to the world and can be called via URL by simply putting a ".xml" at the end of the URL and passing appropriate parameters....