ruby

ruby Open URL and rescue

Hello, I would like to check if a few URL's exst on my old website and collect URLS that returns 404. @paintings = Painting.find(:all) @paintings.each do |painting| open("http://www.mydomain.com/" + painting.user.username.downcase + "/" + painting.permalink) rescue OpenURI::HTTPError @failure += painting.permalink else ...

`gem install mongrel` fails with ruby 1.9.1

I initiated myself into rails development yesterday. I installed ruby 1.9.1, rubygems and rails. Running gem install mongrel worked fine and ostensibly installed mongrel too. I am slightly puzzled because: script/server starts webrick by default which mongrel returns nothing locate mongrel returns lots of entries like /Developer/SDK...

How to download a picture using ruby

Let's say that I want to download this picture using ruby. How do I do that? http://farm1.static.flickr.com/92/218926700_ecedc5fef7_o.jpg I am using mac. ...

Is there a .NET equivalent for Groovy and Grails or Ruby on Rails?

Recently I've been playing a bit with Groovy and Grails and I have to say I'm nothing but impressed by the simplicity and productivity this framework could offer. In the past I've also greeted the world via Ruby on Rails and what not, having that very same feeling so it's pretty much obvious that when it comes to web apps, DRY KISS is d...

Get the value of an instance variable given its name

In general, how can I get a reference to an object whose name I have in a string? More specifically, I have a list of the parameter names (the member variables - built dynamically so I can't refer to them directly). Each parameter is an object that also has an from_s method. I want to do something like the following (which of course do...

How to get multiple-line user input in Ruby?

Hey guys, I know this is a really simple question but my google-fu is failing me. I want to get multiple line user input in Ruby, as gets only gives you one line of input. I want to be able to store this multiple line input into a string so i can output the string to the end of a file. Is there a command available to do this? Thanks in...

How to combine ActiveRecord objects?

I'm wondering if there's an efficient way to combine the results of multiple ActiveRecord objects in Rails. For example, I might make three individual calls to three individual tables, and I want the results combined, and sorted by a common column. Here's a super basic code example that will hopefully make my question easier to understa...

Problem with XML and HTTParty

Hi! I've just started using HTTParty, and i've encountered a problem in the way it builds the Hash from the XML replied by the server. If i setup the following Builder template on the server: xml.thunt :sendSubscriptionResult, :"xmlns:thunt" => "http://example.com/thunt", :status => @status everything works well, i.e. the Hash built...

Changing the column name of a rails object

What if I want to refer to a column of an ActiveRecord object by a different name? for example, I might call: @posts = Posts.find(:all, :select => "created_on") But instead of referring to @posts["created_on"], I'd like to refer to it as @posts["date"]. What would you recommend? Thanks! ...

Support for IMAP IDLE in ruby

Ok, I have been suck on it for hours. I thought net/imap.rb with ruby 1.9 supported the idle command, but not yet. Can anyone help me in implementing that? From here, I though this would work: class Net::IMAP def idle cmd = "IDLE" synchronize do tag = generate_tag put_string(tag + " " + cmd) put_string(CRLF)...

Returning data from forked processes

If I do Process.fork do x end how can I know what x returned ? (e.g. true/fase/string) ? (writing to a file/database is not an option...) ...

Rails and XML

Hi So I have a function that outputs an xml string that goes something like <expensesAC> <cashflow> <month>6</month> <cash>300</cash> <projected>null</projected> </cashflow> <cashflow> <month>6</month> <cash>300</cash> <projected>null</projected> </cashflow> <cashflow> <month>6</month> <cash>300...

Ruby round float to_int if whole number

In ruby, I want to convert a float to an int if it's a whole number. For example a = 1.0 b = 2.5 a.to_int_if_whole # => 1 b.to_int_if_whole # => 2.5 Basically I'm trying to avoid displaying ".0" on any number that doesn't have a decimal. I'm looking for an elegant (or built-in) way to do def to_int_if_whole(float) (float % 1 ==...

The shape inheritance example and "The Ruby way"

In my quest to transition from a decade of C++ to Ruby, I find myself second guessing how to accomplish the simplest things. Given the classic shape derivation example below, I'm wondering if this is "The Ruby Way". While I believe there's nothing inherently wrong with the code below, I'm left feeling that I'm not harnessing the full p...

Ruby: obtaining number of block parameters

Hi there. I have a rather unusual use case whereby I need to be able to obtain the number of parameters a given block has been defined to take. For example... foobar(1,2,3) { |a, b, c| } def foobar(x, y, z, &block) # need to obtain number of arguments in block # which would be 3 in this example end From what I understand, this is...

How do I get rspec / autotest going on a rails project?

What's the best practice for getting RSpec / Autotest going on a Rails project? I googled some stuff but it feels like it may be out of date. ...

Parsing an xml doc with <% %> tags in ruby

I'm looking for a way to parse an xml/html document in ruby, which contains ERB style tags <% %> with ruby code inside. REXML, the built in XML parser won't allow me to do this. I'm aware that I might be able to with a third party library like hpricot, but I'd like to avoid any external dependencies. Is there a way I could get REXML t...

Best Way to Abstract Initializing Attributes

What's the best way to abstract this pattern: class MyClass attr_accessor :foo, :bar def initialize(foo, bar) @foo, @bar = foo, bar end end A good solution should take superclasses into consideration and be able to handle still being able to have an initializer to do more things. Extra points for not sacrificing performance...

Is there a rails trick to adding commas to large numbers?

Is there a way to have rails print out a number with commas in it? For example, if I have a number 54000000.34, I can run <%= number.function %>, which would print out "54,000,000.34" thanks! ...

Deploying Compojure/Sinatra Applications.

What is the preferred way of deploying a compojure/sinatra applications? I have multiple sites running on the host so i can't run jetty/mongrel on port 80. How should i handle multiple sites/applications running at the same host? ...