ruby

`var = something rescue nil` behaviour

In ruby you can throw a rescue at the end of an assignment to catch any errors that might come up. I have a function (below: a_function_that_may_fail) where it's convenient to let it throw an error if certain conditions aren't met. The following code works well post = {} # Other Hash stuff post['Caption'] = a_function_that_may_fail resc...

Only some windows shell commands work via ruby?

Hi. I'm trying to use a script to control my power options since XP doesn't give you an intuitive way to change CPU Frequency options. Here's my script so far: meh = `cmd.exe /C POWERCFG.EXE /QUERY Portable/Laptop` puts "" puts meh case input when 1 then system('cmd.exe /C POWERCFG.EXE /CHANGE Portable/Laptop /processor-thr...

Post request with body_stream and parameters

Hello, I'm building some kind of proxy. When I call some url in a rack application, I forward that request to an other url. The request I forward is a POST with a file and some parameters. I want to add more parameters. But the file can be quite big. So I send it with Net::HTTP#body_stream instead of Net::HTTP#body. I get my request a...

Right usage of ruby global variable $*

Hello, I was wondering the right usage for global variable $*. If the program has puts $*[0] it works great. On the contrary, if I use it as follows: for i in 2..$*[0] a bad value for range (ArgumentError) is throw. Any advise is appreciated ...

Rails caches_page :index in Wrong Location

I have a controller Projects in my Rails app with: caches_page :index However, instead of the cached file being generated at /public/projects/index.html it is located at /public/projects.html. The web server (currently Mongrel) looks for */ directories before *.html files. So the http://…/projects request is routed through Rails and ...

Ruby zlib deflate massive data

I'm trying to use Zlib::Deflate.deflate on a massive file (4 gigs). There are obvious problems with doing that, the first of which being that I can't load the entire file into memory all at once. Zlib::GzipWriter would work, since it works with streams, but it's not zlib compression. Any ideas? ...

How do I setup a custom Gem.path using JRuby::Rack?

Hi Nick et al, I've been having some fun looking at to source code of JRuby-Rack and Rubygems to try to figure out how to solve a org.jruby.rack.RackInitializationException: no such file to load -- compass in my rackup script cased by require 'compass'. I'm passing in a custom 'gem.path' as a servlet init parameter and it is being c...

Custom form helpers

Is there a way that I can create a custom form helper so that instead of: special_field_tag :object, :method I can achieve something like: form.special_field :method ...

Background processing in rails

Hi, This might seem like a FAQ on stackoverflow, but my requirements are a little different. While I have previously used BackgroundRB and DJ for running background processes in ruby, my requirement this time is to run some heavy analytics and mathematical computations on a huge set of data, and I need to do this only about the first 15...

Rails 3 Beta 2, Haml, Nested Layouts and LocalJumpError

Alright, I'm trying to create an app with nested templates. I'm using Rails 3 Beta 2 and Haml. I've poked around and I've decided to take the clearest approach and have structured my templates like so: # application.html.haml !!! %body %h1 Outermost Template = yield(:foobar) # inner.html.haml - content_for :foobar do %h2 Inner Te...

Rendering 'belongs_to" in index view question - Ruby on Rails

I have created a simple blog application with Ruby on Rails. The applications consists of two tables, posts and comments. Comments belongs_to :post and posts has_many :comments. I created posts table with the following columns: title:string, body:text. I created the comments table with the following columns: body:text post_id:integer n...

How do I order by foreign attribute for belongs_to reference where there are 2 keys to foreign table

I have a Model which has a belongs_to association with another Model as follows class Article belongs_to :author, :class_name => "User" end If I wanted to find all articles for a particular genre ordered by author I would do something like the following articles = Article.all(:includes => [:author], :order => "users.name") Howeve...

Rails helper module undefined method `sort'

I'm trying to create a simple helper module in rails, and I'm stumped on the following error message from my new person form (app/views/people/new.html.erb): undefined method `sort' for 97:Fixnum Extracted source (around line #17): 14: <p> 15: <% nations = { 'United States of America' => 'USA', 'Canada' => 'Canada', 'Mexico'...

Is there an existing gem or script that converts numbers to comp-3/packed decimal format?

Hi There, Continuing with my adventure to convert COBOL to a Ruby program, I have to convert a decimal digit to a comp-3/packed decimal format. Anyone know of a simple Ruby script or gem that does this? Berns ...

Detect number of IDLE processors ruby

Hello, I work on shared linux machines with between 4 and 24 cores. To make best use of them, I use the following code to detect the number of processors from my ruby scripts: return `cat /proc/cpuinfo | grep processor | wc -l`.to_i (perhaps there is a pure-ruby way of doing this?) But sometimes a colleague is using six or eight of t...

Ruby: Continue a loop after catching an exception

Basically, I want to do something like this (in Python, or similar imperative languages): for i in xrange(1, 5): try: do_something_that_might_raise_exceptions(i) except: continue # continue the loop at i = i + 1 How do I do this in Ruby? I know there are the redo and retry keywords, but they seem to re-execu...

Ruby: Any gems for threadpooling?

Is there a gem for threadpooling anyone can recommend? ...

Ruby's to_s method question (from Axe book 2nd edition)

According to the axe book (2nd edition), we can use to_s as follows. class Song def to_s "Song" end end song = Song.new() song.to_s But, it doesn't give me anything, in order to print something to the stdout, I should run def to_s p "Song" end Is there any change since the ruby 1.8.2 when the book was written f...

What's the __repr__ equivalence in ruby?

The __repr__ function of python is fancy as it is called when print OBJECT is used automatically. Is there a ruby equivalence for it? I thought it was to_s, but, I had p OBJECT doesn't seem to call the to_s method. === Added === I got something wrong, p OBJECT seems to call to_s method as follows. I got some hints from my the answer...

Ruby Programming Techniques: simple yet not so simple object manipulation

Hi, I want to create an object, let's say a Pie. class Pie def initialize(name, flavor) @name = name @flavor = flavor end end But a Pie can be divided in 8 pieces, a half or just a whole Pie. For the sake of argument, I would like to know how I could give each Pie object a price per 1/8, 1/4 or per whole. I could d...