ruby

Why doesn't my upload from Flash to Rails & attachment_fu work (it has something to do with the content type)?

I am trying to upload a file from a flash widget to my rails application, which uses attachment_fu to handle uplaoded images. I am using flash to upload since it makes it easy to select and upload multiple files. However, I am getting this error when the rails controller tries to call save! on the newly created ActiveRecord object: Ac...

Ruby on Rails: link updates DB

Simple RoR question...I am learning ROR and am making a simple voting app. The candidates are listed in a table and have upvote/downvote links next to their name. I am trying to make it so all the user does is click the link, the vote count is updated, and they are redirected to the initial page. I am not using scaffolding. For some re...

Sending messages between users

I am building an application and it needs to have a feature whereby one user can send another user a message. The system can be the most basic type available in rails and that would suit me fine. Any ideas on how to go about this? Thanks. ...

How can I make a simple text editing application in Shoes?

I am trying to write a simple tool using Shoes. This will indent code for an obscure scripting language we use. It has one large text box and one button. I have the program working on the command line, but am having no luck wrapping this up in Shoes. If anyone could give a working example of an app that does the following tasks to get me...

How do I get Nokogiri to understand my namespaces?

I have the following XML document: <samlp:LogoutRequest ID="123456789" Version="2.0" IssueInstant="200904051217"> <saml:NameID>@NOT_USED@</saml:NameID> <samlp:SessionIndex>abcdefg</samlp:SessionIndex> </samlp:LogoutRequest> I'd like to get the content of the SessionIndex (that is, 'abcdefg') out of it. I've tried this: XPATH_QUE...

Parallelizing Ruby reducers in Hadoop?

A simple wordcount reducer in Ruby looks like this: #!/usr/bin/env ruby wordcount = Hash.new STDIN.each_line do |line| keyval = line.split("|") wordcount[keyval[0]] = wordcount[keyval[0]].to_i+keyval[1].to_i end wordcount.each_pair do |word,count| puts "#{word}|#{count}" end it gets in the STDIN all mappers intermediate values. Not f...

How to react to Excel events in Ruby?

I can find 10^1000 examples of scripting Excel using Ruby, but I can't for the life of me figure out how to have Ruby react to events in Excel. I'm trying retrieve the contents of a row in a worksheet when it's selected, but such an event-based retrieval I can't find any methods or examples for. ...

Better way to fill a Ruby hash?

Is there a better way to do this? (it looks clunky) form_params = {} form_params['tid'] = tid form_params['qid'] = qid form_params['pri'] = pri form_params['sec'] = sec form_params['to_u'] = to_u form_params['to_d'] = to_d form_params['from'] = from form_params['wl'] = wl ...

How do I write a Ruby method to handle zero, one, or many inputs?

I've got a Ruby method like the following: # Retrieve all fruits from basket that are of the specified kind. def fruits_of_kind(kind) basket.select { |f| f.fruit_type == kind.to_s } end Right now, you can call this like: fruits_of_kind(:apple) # => all apples in basket fruits_of_kind('banana') # => all bananas in basket and s...

Ruby on Rails regex

I have these statements in a model: before_save :add_http protected def add_http if (/^http\:\/\/.+$/.match(url)) == nil str = "http://" + url url = str end end I have checked the regex in the console and it appears to be right, but when the 'url' gets saved to the db the "http://" has not been added. Any ideas? ...

Rails i18n Config File Management Best Practices

I'm in the process doing an i18n conversion of a RoR website. I'm using Sven Fuchs textmate bundle along with NewDesk's translate plugin. I've started with the yaml files provided by Sven Fuchs here (http://github.com/svenfuchs/rails-i18n/tree/3e1994d137e1785689e39f6e957087d3baed0011/rails/locale) I'm rapidly seeing keys getting out of ...

Is there a good cached memoization plugin for rails?

I have a model along the lines of: class Account < ActiveRecord::Base has_many :payments has_many :purchases def balance payments.sum(:dollar_amount) - purchases.map{|p| p.dollar_amount}.sum end end I want to memoize the balance method and store it in memcached. The problem, of course, is that the cached value needs to...

Advice for Windows system scripting+programming

I just got a project where I have to do the following on a Windows OS: detect how many drives (C: D: E: ..etc) are connected to current system what the system labels are for each volume how much storage (both used and free) for each of the drives what format each drive is (NTFS/FAT32) how many files are in a given directory in any o...

Practical GUI toolkit?

I am thinking about cross-platform with nice programming language bindings (Java, Ruby and Python). What would be the "flattest" learning curve but yet enough powers to perform most of the standard GUI features? What would you guys/gals recommend; FOX, wx, Tk or Qt? ...

find in files using ruby or python

A popular text editor has the following "find in files" feature that opens in a dialog box: Look For: __searchtext__ File Filter: *.txt; *.htm Start From: c:/docs/2009 Report: [ ] Filenames [ ]FileCount only Method: [ ] Regex [ ]Plain Text In fact, several popular text editors have this. I would...

How to call an attr_accessor attribtue inside the class (but outside any methods)?

I'm using HTTParty for my class, and I want to use the default_params method. However, I also want to be able to set attributes when instantiation my class with initialize. class MyClass include HTTParty attr_accessor :param1, :param2 # This or @param1 doesn't work. default_params :param1 => self.param1, :param2 => self.param2...

Best practices for using memcached in Rails?

Hello everybody, as database transcations in our app are getting more and more time consuming, we have started to use memcached to reduce the amount of queries passed to MySQL. All in all, it works fine and really saves a lot of time. But as caching was "silently appearing" as a workaround to give the app more juice, a lot of our mode...

Unable to combine English words from letters by Ruby

I need to find all English words which can be formed from the letters in a string sentence="Ziegler's Giant Bar" I can make an array of letters by sentence.split(//) How can I make more than 4500 English words from the sentence in Ruby? [edit] It may be best to split the problem into parts: to make only an array of words with...

translating blocks and statements for a DSL

I want to write a simple Ruby DSL to translate some statements and expressions into another language. A basic example would be: some_function { t + 2 } Here, t is not a ruby variable and thus the block can't (and must not!) be evaluated by Ruby. So my best bet would be to use the parsing output (or AST) to do the translation mysel...

Updating the db 6000 times will take few minutes ?

I am writing a test program with Ruby and ActiveRecord, and it reads a document which is like 6000 words long. And then I just tally up the words by recordWord = Word.find_by_s(word); if (recordWord.nil?) recordWord = Word.new recordWord.s = word end if recordWord.count.nil? recordWord.count = 1 else recordWord.count += 1 end r...