ruby

Ruby: Checking if file exists on a remote server.

I am trying to write a script to check if a given URL exists and return itself, if it exists. The file types will be .jpg and .mov. I am currently using the open-uri std library but I'm pretty sure I should be using something else. begin if (open(image_url).read) puts image_url end if (open(video_url).read) ...

How do I get Rails to stop re-loading my gems?

I have a gem: # in /Library/Ruby/Gems/1.8/gems/my_gem-1.0.0/lib/my_gem.rb module MyGem def do_stuff .. end end And I loaded it in Rails: # in [rails_root]/config/environment.rb: config.gem 'my_gem', :version => '1.0.0' And used it: # in [rails_root]/app/controllers/application_controller.rb class ApplicationController < Ac...

transform datetime into local time in Ruby on Rails

Right now I have: Time.strftime("%I:%M%p") which gives me the hr:min AM/PM format which I need. However it's coming back in UTC and I need it local time zone. How do I change it to local time zone and keep that same format for time? ...

is Ruby here to stay?

Ruby on Rails has made a rapid rise as decent language for webdevelopment. How does it compare to other languages in that area (PHP, Perl, Java, VB.NET, ...) from a webdevelopment perspective? Will the number of sites programmed with Ruby keep rising or has its popularity brought up issues that may keep it from becoming a keeper (eg spee...

Do you find convention over configuration good or bad?

I did a fair amount of experimenting with Ruby on Rails and its convention over configuration, which gave me a lot of functionality automatically. But later I found that I wasn't sure I like the paradigm. It seemed to get in the way when dealing with my database and having to make sure everything was named just so, etc. Is there a hug...

How do I get TextMate to use Ruby on Rails by default?

How do I make TextMate use the Rails bundle by default? On some files it's enabled, and on others (including models) it's not, and I wish it would just assume that every .rb file is a Ruby on Rails file unless I tell it otherwise. Is there a project-level or global setting for this? ...

How do I apply a drop shadow to thumbnails using imagemagick and paperclip?

I would like to alter the processing of thumbnails in paperclip by having imagemagick apply a drop shadow to all the thumbnails. What I'm stuck on is the actual imagemagick command that would pull this little miracle off. Everything I've tried returns an incorrectly scaled drop shadow without the original image. def transformation_com...

How do I store an instance variable across multiple actions in a controller?

Say I want to store some variable in my controller. I want to initialize it in one action, increment it in another, and read it in yet another. Just declaring this variable with @foo doesn't work because @foo dies after the action that created it is rendered. I do not want this variable to be stored in a model. Is there a way to pre...

Polymorphic Association with multiple associations on the same model

I'm slightly confused about a polymorphic association I've got. I need an Article model to have a header image, and many images, but I want to have a single Image model. To make matters even more confusing, the Image model is polymorphic (to allow other resources to have many images). I'm using this association in my Article model: cla...

Having to call "render :layout => false" to correctly render js.erb template in Rails 2.3.3

I'm running the latest Rails 2-3-stable branch (currently 2.3.3). I'm using JQuery to post an AJAX request to my 'create' action, in which I have the following block: respond_to do |format| format.js end I have created create.js.erb and to test this action, I've added the following single line: alert('hello'); The request correc...

DRYing my has_many's in Rails

I have a model class that has, among other things: class Group < ActiveRecord::Base has_many :subscriptions has_many :users, :through => :subscriptions has_many :admins, :through => :subscriptions, :source => :user, :conditions => "subscriptions.role = #{ROLES[:admin]}" has_many :subscribers, :through => :subscriptions, :source...

Rails partials and HAML

Hey, I have a ajax method that renderes a @collection of users. This code is from the users partial. Now what I need to do is wrap a ul tag around the 3 li tags for every third object. How do I do this with HAML? I can't just add the %ul tag to "when 1" - because HAML closes the tag when that object has been rendered. -case user_count...

RSpec: stubbing Kernel::sleep ?

Is there a way to stub Kernel.sleep in an rspec scenario? ...

How do you concisely get the average house price from an array of houses?

Assuming the @houses array is set up as follows: house1.price = 10 house2.price = 20 house3.price = 30 @houses << house1 @houses << house2 @houses << house3 This is the starting point of our calculation and we want to find the average price of a house: total = 0 average = 0 for h in @houses total += h.price end average = total/@hous...

Dynamic find conditions in active record

I have an index action in rails that can handle quite a few params eg: params[:first_name] # can be nil or first_name params[:age] # can be nil or age params[:country] # can be nil or country When finding users I would like to AND all the conditions that are not nil. This gives me 8 permutations of the find conditions. H...

Ruby code to JAR

Hi, I'd like to be able to compile a ruby program to a java JAR program. I've looked into JRuby and seen several examples of Java applications that would be able to eval() ruby code, but is there a more elegant solution allowing to simply code everything in ruby and then compile the lot directly to a JAR file? The overall goal behind t...

Run rake task in controller

Hi all, I'd like to run a rake task in my controller. Is there any way to do this? Thanks, Sophy, ...

How can I create a new Date with just a weekday number?

I'm using Date.wday to get the weekday number, but then how can I create a new Date with just that number (without using Date.commercial)? I only need the date to be next week's. For example, if I have 2 (Tuesday), the new date would be whatever date next tuesday is (2009-07-28). Thanks! ...

testing with before_filter

Hi all i've developed before_filter - http://gist.github.com/138659 and in tests like test "should get index" do get :index assert_response :success assert_not_nil assigns(:articles) end i see test_should_get_index(ArticlesControllerTest): NoMethodError: You have a nil object when you didn't expect it! You might hav...

Sort ruby array by updated_at

Hello, all I want to do, is to fetch the lastest item from my models and have them in an array sorted (freshest items first) by the attribute "updated_at". Somewhere is an error, but I can't find it: @results = Array.new Note.find(:all, :limit => 3, :order => "created_at DESC").each do |item| @results << item end Pictur...