ruby

What is the best way to avoid code duplication between views and controllers in Ruby on Rails?

I currently have code in my ApplicationController to check if a user is logged in and has the required access to perform a given action (the tests take place in a before_filter). I require the same functionality in the views to decide if I should be showing the admin links in a list view, but how do I best avoid duplicating code in the ...

Cool way to call methods on multiple objects in Ruby?

A long time ago I saw this trick in Ruby. Instead of doing (for example) if array1.empty? and array2.empty? and array3.empty? You could call all of the objects at once and append the operation at the end, kind of like if %w(array1 array2 array3).each { |a| a.empty? } But I think it was simpler than that... or, it could be that. I r...

284 could not be located in the dynamic link library SSLEAY32.dll

Postgres 8.3 is installed on a windows 2008 server. Ruby 1.8-6 installed. gem install ruby-postgres. When trying a simple connect I get ruby.exe - Ordinal Not Found The ordinal 284 could not be located in the dynamic link library SSLEAY32.dll. OK There seems to be some conflict betweeen the ssleay32 shipped with postgres 8.3 an...

String concatenation and Ruby

Hi, I am looking for a more elegant way of concatenating strings in Ruby. I have the following line: source = "#{ROOT_DIR}/" << project << "/App.config" Is there a nicer way of doing this? And for that matter what is the difference between << and +? Thanks ...

RSpec mocking an :each block

I want to use RSpec mocks to provide canned input to a block. Ruby: class Parser attr_accessor :extracted def parse(fname) File.open(fname).each do |line| extracted = line if line =~ /^RCS file: (.*),v$/ end end end RSpec: describe Parser before do @parser = Parser.new @lines = mock("lines") @lines...

Is there a better HTML escaping and unescaping tool than CGI for Ruby?

CGI.escapeHTML is pretty bad, but CGI.unescapeHTML is completely borked. For example: require 'cgi' CGI.unescapeHTML('&#8230;') # => "…" # correct - an ellipsis CGI.unescapeHTML('&hellip;') # => "&hellip;" # should be "…" CGI.unescapeHTML('&#162;') # => "\242" # correct - a cent CGI.un...

How do I calculate a String's width in Ruby?

String.length will only tell me how many characters are in the String. (In fact, before Ruby 1.9, it will only tell me how many bytes, which is even less useful.) I'd really like to be able to find out how many 'en' wide a String is. For example: 'foo'.width # => 3 'moo'.width # => 3.5 # m's, w's, etc. are wide 'foi'.width...

In Ruby, how does one get their IP octet without going through DNS?

I can, on some of my systems, get my IP address (192.68.m.n format) by doing this: addr = IPSocket::getAddress(Socket.gethostname()) ...the trouble is that this only works if the name the local machine uses for itself is the name the DNS server associates with it. How *&#( hard can it be for ruby to just return its primary interface'...

How to turn off "periodically_call_remote"

I have periodically_call_remote updating a div (main_div) in my web app. This main_div contains links that the user can click that invokes an action that overwrites data within main_div. My problem is that the timer is running on the periodically_call_remote function and even though the user has navigated away from the page, that functi...

How can I refactor this piece of Ruby code to remove duplication ?

Hi, I do not have problem as such but I am quite new to Ruby. I have the following 3 repeatable bits of code in one method and I would like to know how a true Rubyist would first of all remove the duplication and secondly make it more reusable. Here is the code in question: file = File.new( destination) doc = REXML::Document.new file...

Rails active_resource require JS to do delete by link_to?

When I use link_to :method=>:delete, what rails has generated is a javascript click, which is not nice if you do come across situation your client just have javascript disabled. That seems a bad idea to me. Any chance to get around this? ...

What is the best way to properly get to know the Rails framework?

I have been doing two projects on Rails which have worked out quite nicely but have in the process reinvented the wheel, running (and hot) water and painkillers which are as I have learned subsequently already present in the framework. So basically, what is the best way to properly get know all the clever parts in the framework that wil...

How to force ruby to show full stack trace?

I just got this error message: ... from c:/ruby/lib/ruby/gems/1.8/gems/... ... 10 levels... from c:/ruby/lib/ruby/gems/1.8/gems/... ... and the bug (of course) is hidden somewhere in ... 10 levels.... How to force ruby to show full stack trace? ...

Ruby (Shoes) - Wait for function to return value

I have a function that presents the user a combo-box. def select_interface(interfaces) list_box :items => interfaces do |list| interface = list.text end ### ideally should wait until interface has a value then: ### return interface end The rest of the program depends on the selection from this combo-box. I would like to f...

How do I catch redirections to other domains when testing with Webrat?

In my Rails app, I have a form which redirects through a foreign service, Amazon FPS. The form POSTs to an action in my app which redirects to Amazon, who collect information and then redirect back to my app. I'm testing this workflow with Webrat. Obviously I can't test Amazon, so I want to check that the redirection to Amazon happens...

When would you use Infinity?

So in Ruby there is a trick to specify infinity: 1.0/0 => Infinity I believe in Python you can do something like this float('inf') These are just examples though, I'm sure most languages have infinity in some capacity. When would you actually use this construct in the real world? Why would using it in a range be better than just us...

Common Programming Mistakes for Ruby Developers to Avoid

Possible Duplicate: What are the Ruby Gotchas a newbie should be warned about? What are some common programming mistakes made by Ruby developers, and how can they be avoided? For example, requiring a gem and forgetting to require 'rubygems'. (Inspired by Common programming mistakes for .NET developers to avoid?) ...

Can I find the file/linenumber where a method was defined?

Duplicate: How To Find Where A Ruby Method Is Defined At Runtime With a given object, is it possible to find out where each method was defined? ...

How do I do lots of processing without gobbling cpu?

I know the question title isn't the best. Let me explain. I do a TON of text processing which converts natural language to xml. These text files get uploaded fairly fast and thrown into a queue. From there they are pulled one-by-one into a background worker that calls our parser (using boost spirit) to transform the text into xml and lo...

What is the best way to determine which source files are no longer needed?

I am now responsible for a Rails application that was built in a very quick-and-dirty fashion. It has many view files (html templates) that are not used. It also has many css files that are not used. What is the best way to determine which files are no longer needed so they can be deleted? I'm looking for a generic solution and not a R...