ruby

Ruby datetime suitable for mysql comparison

Rails has been good with automatically inserting correctly formatted datetimes in MySql without the need for me to give it much thought. However, for doing a validation, I need to check if a stored mysql datetime value (ie 2008-07-02 18:00:00) is greater than or less than "now". I can call DateTime.now or Time.now but how can I convert...

How to check if a custom exception is raised in a Ruby app?

I have a test class: require File.dirname(__FILE__) + '/../car.rb' class CarTest < Test::Unit::TestCase def test_set_color assert_raise InvalidColorEntry "Exception not raised for invalid color" do Car.set_color("not a color") end end end InvalidColorEntry is an exception class that I placed in the car.rb f...

Question about form

What does the [@post, Comment.new] construction literally mean? I'm using it (saw in a podcast, or somewhere else) but I still don't quite understand what does it mean? What literally happens if we hit Submit button (sure, the comment is added, but I'm interesting about the inner mechanics and how is it connected with mentioned construct...

Possible to change RAILS_ROOT?

before the moans and groans, my client is using Godaddy to host his rails app. I have managed to get the app working for him, but Godaddy uses a sym link to point to the app which appends the root of the website ex. www.yoursite.com/myrailsapp I have tried putting this in the environment.rb Basically all the links is suppose to work ...

How to efficiently merge two hashes in Ruby C API?

I am writing a C extension for Ruby that really needs to merge two hashes, however the rb_hash_merge() function is STATIC in Ruby 1.8.6. I have tried instead to use: rb_funcall(hash1, rb_intern("merge"), 1, hash2); but this is much too slow, and performance is very critical in this application. Does anyone know how to go about perf...

One-to-many Associations Empty Columns Issue (Ext on Rails)

I'm playing with rewriting part of a web application in Rails + Ext. However, I'm having trouble getting an associated models' name to display in the grid view. I've been able to successfully convert several models and arrange the views nicely using tabs and Ext's layout helpers. However, I'm in the middle of setting up an association ...

How to check if an element in one multi-dimensional Ruby array exists in another?

I am trying to determine if the an element in one multi-dimensional array exists in another similarly structured array. suspects = [['Rod', 100], ['Jane', 75], ['Freddy', 125]] criminals = [['Bill', 75], ['Ted', 50], ['Rod', 75]] The response I am looking for is either true or false. In the example above the response would be true bec...

Unable to catch an exception in Ruby

I have something like this: class Vehicle def self.set_color(input) if %w{blue red green}.include?(input) input else raise "Bad color" end end end class Car < Vehicle def make_car begin my_color = Vehicle.set_color("orange") rescue puts "you screwed the pooch" end...

Test case problem when exception handled

I have the following code with the corresponding test case: class XXX attr_accessor :source def check begin raise ArgumentError, "No source specified." \ unless @source.empty? != true puts "Passed" rescue print "Error: ",$!, "\n" end end end class T...

Watir image processing

Is there a way to get an image extension (based on the content-type header) and it's body in Watir? Here is an example require 'watir' zz = Watir::IE.new zz.goto('http://flickr.com') image = zz.image(:src => %r/l.yimg.com\/g\/images\//) puts image I need to get extension and the contents (base64encoded or just location of a temp fil...

Tricky transform of hash -> array

I have a structure like this: {:foo => ['foo1', 'foo2'], :bar => ['bar1']} Which I would like to have transformed into: [[:foo, "foo1"], [:foo, "foo2"], [:bar, "bar1"]] My current solution is imperative: result = [] h.each do |k,v| v.each do |value| result << [k, value] end end While this works, I am certain that there i...

How to install RMagick gem in Ruby 1.8.6 MingW?

Do you have a detailed guide for this? Thanks a lot :) ...

How to highlight all instances of a variable in TextMate?

If you place the cursor on a variable in NetBeans (at least in Ruby code) it highlights all instances of that variable. This is a feature I like very much. Does TextMate or some bundle have a similar feature? ...

AR_mailer throwing exception. Mail still sent.

I'm setting up AR_mailer to send mail. All works from a sending point of view, but when the mail is actually created I receive this error: A LoadError occurred in users#create: Expected /usr/lib/ruby/gems/1.8/gems/adzap-ar_mailer-2.1.5/lib/action_mailer/ar_mailer.rb to define ActionMailer::ARMailer [RAILS_ROOT]/vendor/rails/activesup...

Working with datetime in Rails

How to properly extract a time only from the datetime object (post.created_at, to be precise) with/without the influence of TimeZone? How to extract the day, month, year? How to extract the day/month/year via a custom pattern (is it possible)? ...

test if a PDF file is finished in Ruby (on Solaris/Unix)?

hi, i have a server, that generates or copies PDF-Files to a specific folder. i wrote a ruby script (my first ever), that regularily checks for own PDF-files and displayes them with acrobat. So simple so nice. But now I have the Problem: how to detect the PDF is complete? The generated PDF ends with %%EOF\n but the copied ones are ge...

Ruby gem listed, but wont load (gem in user dir, not ruby dir)

I'm trying to get some gems working on a web-host which supports ruby and some ruby gems, but not some of the ones I need to use. Following the instructions I found here, I kept the original gem location in my gem path, and added my own at /path/to/my/home/gems to ~/.gemrc gemhome: /users/home/myuser/gems gempath: - /usr/local/lib/ruby...

How do I figure out what this character is?

Update: Apparently these are control characters, not Unicode characters. I'm trying to parse an XML file which has an odd character in it that makes it invalid and is causing my tools (Firefox, Nokogiri) to complain. Here's what the character looks like in Firefox, and what it looks like when I copy and paste it into Textmate (I'm on O...

Get a Time object representing a given time on a given date in Ruby

What's the easiest way to give a Time object representing a given time (say 5pm) on a given date (say 1/1/2010) in a given time zone (say EST)? ...

What's the difference between DateTime and Time in Ruby?

And what factors would cause me to choose one or the other? ...