ruby

RFC2616 : Do I really need to set WWW_Authenticate when returning 401?

According to RFC2616 if I return 401 in response to a request to my (Ruby) server, I "MUST include a WWW-Authenticate header field." Is this really true? Not setting the header seems to have no negative impact. I'm using Merb as a web framework and it doesn't force me to set the header. Am I missing something or is this a rule more hono...

SOAP client generator for ruby/ruby on rails

I have been looking for a fully functional WSDL client generator for ruby. I tried the one called wsdl2ruby and it didn't work. I think it has problems with detecting complex types correctly. Can someone point me to the right library if there is one? I am specifically looking to generate a full functional client for SOAP API provided by...

Automated testing with Ruby on Rails - best practices

Curious, what are you folks doing in as far as automating your unit tests with ruby on rails? Do you create a script that run a rake job in cron and have it mail you results? a pre-commit hook in git? just manual invokation? I understand tests completely, but wondering what are best practices to catch errors before they happen. Let's tak...

Ruby on Rails: models that do not have a table

What's the best way to create a model in Ruby on Rails that doesn't have an underlying implementation in as far as a database table goes? It's very common to write classes that perform behavior on a particular problem domain, yet can use some of the benefits that ActiveRecord has such as validation. Is it best to just create it as a mo...

Ruby number_to_currency displays a totally wrong number!

Greetings! I’m an old Delphi programmer making the leap to the Mac, Ruby, Rails, and web programming in general. I’m signed up for the Advanced Rails workshop at the end of the month. In the meantime, I’ve been working on porting a mission-critical (of course) app from Delphi to RAILS. It feels like I’ve spent most of the past year with ...

How do I combine two methods into one json formatted variable?

I'm storing JSON formatted data into a var adds with the following methods: var adds = <%= raw @add.to_a.to_json %>; var adds = <%= raw @add.nearbys(1).to_json %>; The first line of code stores the location of an individual in JSON format, the second line of code searches for that person's neighbors, within a 1 mile range. How do I c...

IntelliJ and Ruby plugin: Won't load

I'm trying to enable Ruby via the plugin manager, and I receive the following error: Plugin "Ruby" won't be able to load because required plugin "com.intellij.modules.ultimate" is disabled. I'm using the latest community edition. What's going on? ...

ruby super keyword

Hi, From what I understand, 'super' keyword invokes a method with the same name as the current method in the superclass of the current class. Below in the autoload method, there is a call to 'super'. I would like to know in which superclass I would find a method with the same name or what does the call to 'super' do here module ActiveS...

format string (postcode) in ruby

I need to re-format a list of UK postcodes and have started with the following to strip whitespace and capitalize: postcode.upcase.gsub(/\s/,'') I now need to change the postcode so the new postcode will be in a format that will match the following regexp: ^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-...

In Ruby, in the context of a class method, what are instance and class variables?

If I have the following piece of Ruby code: class Blah def self.bleh @blih = "Hello" @@bloh = "World" end end What exactly are @blih and @@bloh? @blih is an instance variable in the class Blah, and @@bloh is a class variable in the class Blah, correct? Does it mean that @@bloh is a variable in Blah's class, Class? ...

Parsing CArchive (MFC classes) files in Ruby

I have a legacy app that seems to be exporting/saving files with CArchive (legacy MFC application). We're currently refactoring the tool for the web. Is there a library I can look at in Ruby for parsing and loading these legacy files? What possible libraries could I look into? Problems with the file format according to XML serializati...

Net::HTTP::Unauthorized - how do I get at the WWW-Authenticate header?

Given the code below ... Net::HTTP.start('localhost', 4000) do |http| # # usual stuff omitted for clarity # @response = http.request(req) end ... if a (well behaved) server returns a 401 (Unauthorized) response, how do I get at the WWW_Authenticate header? The best solution I've got isn't at all good ... class Net:...

class method as hash value

I have this working code: class Server def handle(&block) @block = block end def do @block.call end end class Client def initialize @server = Server.new @server.handle { action } end def action puts "some" end def call_server @server.do end end client = Client.new client.call_server My Ser...

hpricot throws exception when trying to parse url which has noscript tag

I use hpricot gem in ruby on rails to parse a webpage and extract the meta-tag contents. But if the website has a <noscrpit> tag just after the <head> tag it throws an exception Exception: undefined method `[]' for nil:NilClass I even tried to update the gem to the latest version. but still the same. this is the sample code i use. r...

group hash of dates by week/month/year

Given this hash in ruby: h={ 2010-03-01=>2 2010-03-02=>4 2010=03-03=>1 .. .. n days with working hours } where hash key is a date and hash value is an integer, how do I transform this hash into a new hash where keys are weeks/months/years (with aggregation)? Let me show an example with weeks, the end result should look like: h_weeks...

How do I... truncate string in an array

In a ruby on rails app, I build an array of Project Names and project id values, but want to truncate the length of the names. Current code is: names = active_projects.collect {|proj| [proj.name, proj.id]} I have tried to add a truncate function to the block, but am getting undefined method for class error. Thanks in advance - I jus...

How do I handle nils in views?

I have the following models set up: class Contact < ActiveRecord::Base belongs_to :band belongs_to :mode validates_presence_of :call, :mode validates_associated :mode, :band validates_presence_of :band, :if => :no_freq? validates_presence_of :freq, :if => :no_band? protected def no_freq? freq.nil? end def no_ba...

Running rspec against multiple targets

I've written an rspec test using Watir against a web application and it's running fine. However, I now want to be able to run this test against the web application running on different domain names. My initial thought was that I'd be able to pass a value to spec at the command line to set a variable within my script, but I can't see an...

Get number of times in loop over Hash object

I have an object of type Hash that I want to loop over via hash.each do |key, value|. I would like to get the number of times I've been through the loop starting at 1. Is there a method similar to each that provides this (while still providing the hash key/value data), or do I need to create another counter variable to increment within...

Passenger processes restart even though PassengerPoolIdleTime is 0

I have set PassengerPoolIdleTime to 0, with the expectation that this means I can "warm" up a bunch of passenger processes on my server, and the next time I have a burst of traffic (even if it is days later), they will all be warmed up and ready to accept requests. What I'm seeing instead is that every morning when I get up, passenger-s...