ruby

Sequel + Sinatra + Phusion Passenger + MySQL Connection Management

We are using Sinatra and Sequel for a small API implementation. The problem we have however is that on every page request Sequel opens new connections to MySQL, and keeps them open till they timeout, or you restart Apache. There's not a lot of documentation on how to reuse connections, so any help, explanations, and/or pointers in the r...

raise "NullPointerException" in Ruby

I previously worked a lot with Java and now I am working more with Ruby. One thing I cannot figure out though is what is the ruby equivalent to the Java "NullPointerException"? I want to test variables when I enter a function and if they are nil I want to raise this type of exception. Is there a specific ruby error class to raise this...

JRuby with Ruby 1.9

I know there is ongoing work for in this regards but what is the exact status of JRUby 1.3 compatibility with Ruby 1.9.1 ? I am slightly confused if I can dwnload jruby 1.3 and start working with my ruby 1.9 installation. The JRuby homepage says : * Ruby 1.8.6 compatible now * Ruby 1.9 compatible soon Wikipedia says the foll...

Ruby roo Excel.new

Hi, I have a question regarding the ruby roo library. There is a method for opening the Excel document Excel.new . How to close this document, having worked with it? ...

method with same name and different parameters in Ruby

I have this code: def setVelocity (x, y, yaw) setVelocity (Command2d.new(x,y,yaw)) end def setVelocity (vel) ...... end vel is a Command2D class that has 3 attributes, is Comparable and defines + , basically is a convenient class for me to manage those 3 attributes, so I want to use it internally in my library (dont want to make the...

Captcha Implementation in ROR

Do I need to create a database table to implement captcha on a registration form? ...

terminal color in ruby?

hi is there a ruby modul for colorize strings in a linux terminal? ...

I have a problem to round off the poll results to amount to 100%

I am developing a poll using ruby, but the answer that i get is incorrect, i am looking for the answers that will results in 100% but my answers are sometimes 101% or 99% and i am sure is the rounding off of my floats but i just can get them right because without the rounding off the answers are very long floats. my code of calculating t...

can you pass self to lambda in rails?

I want to define a class method that has access to a local variable. So this would be different for each instance of the class. I know you can make a class method dynamic with lambda like when you use it with named_scope. But can this be done for values that are specific to an instance? In detail it is the has_attached_file method for...

Installing Ruby 1.9.1 on Ubuntu?

I wonder about installing the latest version of Ruby on Ubuntu 9.04. Now I can run through the ./configure and make stuff fine, but what I wonder about: how to avoid conflicts with the packaging system? For example if some other package I install depends on Ruby, wouldn't the package manager install the (outdated) Ruby package and in the...

Persistent hashtable for Ruby programs?

I need a small unstructured database for my Ruby scripts. Not Sqlite, something more like a persistent hashtables would work perfectly, as long as it can store basic Ruby structures (arrays, strings, hashes etc. - all serializable) and won't get corrupted when Ruby scripts crash. I know there's plenty of solutions like that for Perl wit...

Construct a valid MySQL query using Ruby/MySQL

If using the Ruby/MSQL library I returned an object of type Mysql::Result, how would I construct a string that loops through the object and constructs a valid MySQL query? I am having trouble figuring out how to drop the final comma from the output below, and how to pass this output back to the query() method. require 'mysql' dbh = Mysq...

How to get an array of table names in ruby

I'm trying to get the output of the query 'SHOW TABLES FROM database_name' into an array using the ActiveRecord database connection. I can't seem to figure out how to do this. can someone please enlighten me? -C ...

Ruby instance methods from a string and a block

Is it possible to define an instance method in ruby from a string (the method name) and a block (the method's contents)? I imagine this will need to use instance_eval(), but I haven't figured out how to mix the two data types yet. Both the string and the block are dynamically determined, so it would work to create the block with the "de...

Transform DateTime into simple Date in Ruby on Rails

I have a datetime column in db that I want to transform into a simple date when I show it to users. How can I do that? def shown_date # to_date does not exist, but is what I am looking for self.date || self.exif_date_time_original.to_date end ...

unpacking/freezing gems into a non-rails ruby app

I'm writing a non-Rails ruby application (gasp!) and would like to be able to include all the gem dependencies which the application requires in a vendor subdirectory. This would be similar to how http://gemsonrails.rubyforge.org/ works for Rails apps. The goal here is to avoid the situation my team currently experiences when a new dep...

Removing duplicates from array before saving

I periodically fetch the latest tweets with a certain hashtag and save them locally. In order to prevent saving duplicates, I use the method below. Unfortunately, it does not seem to be working... so what's wrong with this code: def remove_duplicates before = @tweets.size @tweets.delete_if {|tweet| !((Tweet.all :conditio...

Ruby: split one hash into two based on desired keys

I'm hoping this question has a very simple answer. I can think of ways to do with with boring, annoying looping, but I'm hoping there's a more elegant solution. If I have the following two variables: hash = {:a => 1, :b => 2, :c => 3, :d => 4} keyset = [:a, :c] How can I get the following two hashes in the simplest way possible? has...

Non greedy searches with Hpricot?

I'm using Hpricot for traversing an XML packet. For each node I'm on, I want to get a list of the immediate children . However when using (current_node/:section) I'm getting ALL descendant sections, not just the immediate children. How can I get around this? ...

When would a Ruby flip-flop be useful?

I think I understand how a flip-flop works thanks to this tutorial, but the example there is contrived just for teaching. Can anyone give an example of how you have actually used or would use a flip-flop? Edit: I'm looking for a real-world application, not just another demonstration. What problems can this tool solve? ...