ruby

How to scrape a _private_ google group?

Hi there, I'd like to scrape the discussion list of a private google group. It's a multi-page list and I might have to this later again so scripting sounds like the way to go. Since this is a private group, I need to login in my google account first. Unfortunately I can't manage to login using wget or ruby Net::HTTP. Surprisingly googl...

How do I debug a Net::HTTPInternalServerError error when using Mechanize?

c:/ruby/lib/ruby/gems/1.8/gems/mechanize-1.0.0/lib/mechanize.rb:259:in `get': 500 => Net::HTTPInternalServerError (Mechanize::ResponseCodeError) I get the above error when I try to navigate to the following webpage http://fakewebsite.com//admin/edit_building.cfm?page=buildings&updateMode=yes&id=1251 I can navigate just fine ...

Is there a way to translate C code to Ruby?

I'm sitting with massive amounts of legacy C code that needs to be converted to Ruby for a project. I've seen Ruby to C translators online, but not the other way around. Would there be a simple way to approach this particular problem? ...

Ruby switch like idiom

Hey, I have recently started a project in Ruby on Rails. I used to do all my projects before in Python but decided to give Ruby a shot. In the projects I wrote in Python I used a nice little technique explained by the correct answer in this post: http://stackoverflow.com/questions/277965/dictionary-or-if-statements-jython I use this...

How do I print out the cookies that Mechanized has stored?

I'm using mechanize to login into a website and then retrieve a page. I'm running into some problems and I suspect this is due to some values in the cookies. When Mechanize logs into a website I assume it stores the cookies. How do I print out all the data stored in the cookies by Mechanize? ...

What are the features of dynamic languages (like Ruby or Clojure) which you are missing in Scala?

What do you lose in practice when you choose a statically-typed language such as Scala (or F#, Haskell, C#) instead of dynamically-typed ones like Ruby, Python, Clojure, Groovy (which has macros or runtime metaprogramming capabilities)? Please consider best statically-typed languages and best (in your opinion) dynamically-typed languages...

How to make Nokogiri transparently return un/encoded Html entities untouched?

How can I use Nokogiri with having html entities (like German umlauts) untouched? I.e.: # this is fine node = Nokogiri::HTML.fragment('<p>&ouml;</p>') node.to_s # => '<p>&ouml;</p>' # this is not node = Nokogiri::HTML.fragment('<p>ö</p>') node.to_s # => '<p>&ouml;</p>' # this is what I need node = Nokogiri::HTML.fragment('<p>ö</p>') ...

work with rescue in Rails

Hi, I am working with the following piece; def index @user = User.find(params[:id]) rescue flash[:notice] = "ERROR" redirect_to(:action => 'index') else flash[:notice] = "OK" redirect_to(:action => 'index') end Now I either case whether I have a correct ID or not, I am always getting "OK" in my view, what am I...

Use ruby Timeout class as background thread

I found ruby class Timeout very useful for my project. But i need to run a block of code in background and keep it under a timeout.. For example Timeout::timeout(2) { block.call } How to do that? ...

How do I add text to an empty element in Hpricot?

If I have an empty tag: <tag/> How can I add text so that I end up with: <tag>Hello World!</tag> I can only seem to swap the whole tag with different content or add content before/after it. ...

How should I do a loop a nokogiri search in ruby?

I have the following that I retreive the title of each url from an array that contains a list of urls. require 'rubygems' require 'nokogiri' require 'open-uri' @urls = ["http://google.com", "http://yahoo.com", "http://rubyonrails.org"] @found_titles = Array.new @found_titles[0] = Nokogiri::HTML(open("#{@urls[0]}")).search("title").inn...

Ruby JSON.pretty_generate ... is pretty unpretty

I can't seem to get JSON.pretty_generate() to actually generate pretty output in Rails. I'm using Rails 2.3.5 and it seems to automatically load the JSON gem. Awesome. While using script/console this does indeed produce JSON: some_data = {'foo' => 1, 'bar' => 20, 'cow' => [1, 2, 3, 4], 'moo' => {'dog' => 'woof', 'cat' => 'meow'}} som...

DataMapper: Create new record or update existing

Does DataMapper provide a convenient way to create a new record when none exists or update an existing one? I couldn't find anything in the API documentation. This is what I have at the moment which doesn't seem very elegant: foo = Foo.get(id) if foo.nil? foo = Foo.create(#attributes...) else foo.update(#attributes...) end foo.save...

A reader method that returns the value of an instance variable and then resets it to nil

Is there a shorter (and cleaner) way to write the following reader method: class Foo attr_writer :bar def bar return_value = @bar self.bar = nil return_value end end Here's some output on the console to show what it does: >> foo = Foo.new => #<Foo:0x1010e9cf8> >> foo.bar => nil >> foo.bar = "ok" => "ok" >> foo.bar ...

Rails class << self

Hey. I would like to understand what "class << self" stands for in the next example. module Utility class Options #:nodoc: class << self def parse(args) end end end end Thx! ...

Do I need RubyCocoaBridge or MacRuby or both to develop in Ruby for the iPad?

I've heard both MacRuby and Ruby CocoaBridge discussed in relation to iPhone/iPad development in Ruby. Do I need one or both of these? ...

Erb with Sinatra in ruby

So I have a webserver I've built using sinatra, the meat of which goes like this: set :variable,"value" get '/' do erb :index end And, of course, the template in views/index.erb which looks something like this: <html> <!-- etc --> <ul> <% my_array.each do |thing| %> <%="Something: #{thing}, variable from sinatra: #{settings.v...

ec2_bundle_vol fails with error LoadError

Hi, I am a newbie in amazon ec2 setup. I have now setup a machine to my taste - and I now want to bundle it. I am running the following command from the launched instance - root@domU-21-34-67-26-ED-Z4:~# ec2-bundle-vol -r i386 -d /mnt \ -p ACT-VOL -u 8940-1355-4155 -k /tmp/pk-key.pem \ ...

Error Building Gem

I tried to install the following gem: http://github.com/maxjustus/sinatra-authentication on Windows 7 running Ruby 1.9 from the One-Click Installer. I got the following error: Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\Joel>gem install sinatra-authentication Buildin...

qtruby, QUiLoader and respond_to?

I'm writing a simple Qt4 application in Ruby (using qtruby) to teach myself both. Mostly it has gone well, but in trying to use Ruby's "duck typing" I've run into a snag; respond_to? doesn't seem to reflect reality. irb(main):001:0> require 'rubygems' => true irb(main):002:0> require 'Qt4' => true irb(main):003:0> require 'qtuitools' =...