ruby

Ruby on Rails: Finding all "incomplete tasks" through named_scope

Hello, I have two models: (1) Task, and (2) CompletedTask. I'm looking to do a named_scope for :incomplete_tasks (tasks that don't have any entries in the completed_tasks table) I'm thinking it would be something like this in my Task model: named_scope :incomplete_tasks, :conditions => **completed_tasks.empty?** Do you know how I f...

how to take an subarray, and flatten it back into parent array ?

parent = ["A","B","C",["a","b", "c", "d"], "D"] parent.each do |children| if children.is_a? children.flatten # how do i insert it back to parent # so that this loop can continue looping through the remainder #including the newly flattened children(a,b,c,d) end end The question is, once an array is discovered, i flatten it, and nee...

Ruby/Rails image processing libraries

Good friends of stackoverflow, I am here today for guidance regarding image processing/manipulation using Ruby in a Rails environment. I'm creating on-the-fly dynamic banner ads that will feature mostly (if not completely) text. It's fairly simple with just a line or two, but I'd like the option to adjust font, text color, text size, etc...

how to replace array's element ?

how can i substitue an element's array ? a = [1,2,3,4,5] i need to replace 5 with [11,22,33,44].flatten! so that a now becomes a = [1,2,3,4,11,22,33,44] ...

Passenger, Nginx, and Capistrano - Passenger not launching Rails app at all

Essentially, my route is working perfectly, Passenger seems to be loading - all is hunky-dory. Except that nothing Railsy happens. Here's my Nginx log from starting the server to the first request (ignore the different domain/route - it's because I haven't moved the new domain over yet, and it's returning a 403 error because there's no i...

defined? in Ruby works in irb but not in my class file

Hello, I was writing a small Heap implementation and upon creating my Node class I noticed some weird behaviour. I wanted to call defined?(x) to ensure x was defined, then check if x was an Integer, before storing it in the Node's value class variable. In IRB I can call defined?(x) and the result is nil. However, in the class, I try th...

How to give users a file storage limit?

I'm working on a web application right now using Rails and wanted to see if anyone knew of a good way to keep track of file storage limits? We want to give users a specific amount of room that they can use to upload files and we are using paperclip for storage on Amazon S3. Any thoughts? Thanks ...

Removing XML entities from string in Ruby

Hi, I try to parse RSS chaanal with simple-rss lib. Unfortunately I got a lot of garbage in node: <description>&lt;p&gt; some decryption &lt;/p&gt; &lt;a href="http://url.com/trac/xxx/wiki/foo?action=diff&amp;amp;amp;version=28"&amp;gt;(diff)&amp;lt;/a&amp;gt;&lt;/description&gt; I need to retrieve text ("some description") and o...

Detecting image equality at different resolutions

I'm trying to build a script to go through my original, high-res photos and replace the old, low-res ones I uploaded to Flickr before I had a pro account. For many of them I can just use Exif info such as date taken to determine a match. But some are really old, and either the original file didn't have Exif info, or it got clobbered by ...

Sinatra + Bundler ?

I'm wondering how a one can use Bundler with Sinatra. The idea is to use the gems that Bundler downloads inside the .gems folder. ...

How can I use index or rindex with a block in Ruby?

Is there any Array or Enumerable built-in that allows me to search for an element using a block, and return its index? Something along the lines of : ar = [15,2,33,4,50,69] indexes = ar.find_indexes {|item| item > 4 == 0} # indexes will now contain 0,2,4,5 It would be very easy to add my own, but I'd like to know if this already exis...

Proxying each method in Ruby

Is this the most efficent way to forward the 'each' call to the Hash's native each method. Should I make @index visible to the outside? I'm a bit unsure as a block is invovled. class TimeSlice def initialize(list) # @index is a hash @index = list.do_some_magic() end def each(&block) @index.each(&block) end end ...

Output text size in ruby on rails

How do I get only first (say) 200 characters from my object Article (which is in a table articles with aattributes content and title)? Thank you ...

How can I get a reference to a method that contains the arguments used for invocations, in Ruby?

Given this code: a = {1=>2} m = a.method(:[]) I know that I can now use : value = m.call(1) and it will return 2. The thing is, what do I need to change so that I can call the method directly like : m.call() and it will get the 1 sent as a parameter? It would be nice to be able to write something like : m = a.method(:[],1) # wh...

Selenium in Ruby - can't get it working, and it's reset firefox to it's default installed state

I've been using Selenium (with selenium rc, and the ide) in ruby, with rspec, for some time and it's been great. I recently wiped my installation of ubuntu, however, and installed ubuntu 9.10. I set selenium up again, which consisted of installing the selenium-client gem (1.2.17) and adding the selenium ide plugin in firefox. When i r...

How do I stub/mock a call to the command line with rspec?

I'm trying to test output from a command line tool. How do I 'fake' a command line call with rspec? Doing the following doesn't work: it "should call the command line and return 'text'" do @p = Pig.new @p.should_receive(:run).with('my_command_line_tool_call').and_return('result text') end How do I create that stub? ...

Refactoring ActiveRecord models with a base class versus a base module

Class A and B are identical: class A < ActiveRecord::Base def foo puts "foo" end end class B < ActiveRecord::Base def foo puts "foo" end end What's the difference between refactoring like this with a base class: class Base < ActiveRecord::Base def foo puts "foo" end end class A < Base end class B < Base end versus li...

rails/ruby - how to rescue ActionView::TemplateError

i am testing an application built with rails and from time to time get bugs when it happens, ActionView::TemplateError occurs and gets logged in log/production.log how do i catch it and f.ex. send myself by email whenever it happens somewhere in the app? what is the best way to do that? f.ex. in ApplicationController with some sort o...

How to retrive non standard node from RSS feed in Ruby

Hi I use this parser for my project: http://simple-rss.rubyforge.org/ It works nice but I have nodes like this: <dc:creator>viroos</dc:creator> I have no idea how to get nodes like this. ...

self = Descendant in Ruby?

Hi there, I have a text log from a game with (for example) two types of entries viz. Chat and Event. For the most part they are very similar so I have a LogEntry class defined as so; class LogEntry < Array def initialize(str) super str.split end def parse LogEntry.parse self end def LogEntry.parse(entry) # Proc...