ruby

Shortening socket timeout using Timeout::timeout(n) does not seem to work for me

I found what I thought should work perfectly at http://stackoverflow.com/questions/517219?tab=oldest#tab-top but, it did not work for me. I have Ruby 1.9.1 installed on Windows and, when I try the example "is_port_open" test, it does not work. The socket call still takes around 20 seconds to timeout no matter what value I set for the ti...

In Ruby, can I make a reference to an array offset?

In Ruby, can I do something C-like, like this (with my made-up operator '&'): a = [1,2,3,4] and b = &a[2], b => [3,4], and if I set b[0] = 99, a => [1,2,-9,4]? If the elements of an array are integers, does Ruby necessary store them consecutively in a contiguous part of memory? I'm guessing "no", that only addresses are stored, intege...

Comparing hash of hashes in Ruby

I am having two structures similar to the one below. [1 => [{'pc'=>1,'id'=>0},{'pc'=>4,'id'=>0},{'pc'=>2,'id'=>1]] But both of them need not contain the inside array in the exact same order. How to compare in such case? ...

Need a ruby solution for executing a method in separate process.

I am implementing a poller service whose interface looks like this. poller = Poller.new(SomeClass) poller.start poller.stop The start method is supposed to continuously start hitting an http request and update stuff in the database. Once started, the process is supposed to continue till it is explicitly stoped. I understand that imp...

Offer current date in Rails

I want to offer the current date to the user when a new record is created and I want to allow him to edit the offered date. For example I write a bug tracking system and I have a date_of_detection field. 99% of the time it is good if it is the current date, but for the sake of the 1% the user should be allowed to edit it and set any earl...

View helper with Rails returns the wrong value [Rails, Ruby]

I am trying to write a view helper but it's not returning the correct thing. def table_layout_for(object, *attrs) content_tag :table, :class => "layout" do for a in attrs content_tag :th do object.class.human_attribute_name(a) end content_tag :td do object.send(a) if object.respond_to?(a) en...

Rails problem display attribute key along with attributes value

I have the following problem. I have a form which takes input for a "Chart" object. But after processing the form, i wish to display one of the values, and it adds the key of this value. Class model class Chart attr_accessor :title, :series def initialize(title = nil, series = []) @title, @series = title, series end end ...

Why won't this Ruby each loop break?

I have the following code in one of my personal projects: def allocate(var, value) # Allocate the variable to the next available spot. @storage.each do |mem_loc| if mem_loc.free? mem_loc.set(var, value) # Set it then break out of the loop. break end end end Each item in the storage array is an object that resp...

rubygems setup.rb: no such file of directory

Hello all, I have a small question. When I tried to install rubygems by following the code here: wget 'http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz' tar -vxzf rubygems-1.3.5.tgz cd rubygems-1.3.5 ruby setup.rb But when I try to do ruby setup.rb it says bash: /usr/bin/ruby: No such file or directory . So I tried w...

how to run a spec file with ruby without spec

how to run a spec file with ruby without spec? How do i inherit the spec base class to the current ruby spec file? ...

Ruby, Candy and SQL-like mongo stuff

So Candy is a really simple library for interacting with Mongo in Ruby. My poor SQL brain is having a tough time figuring out how I should map out this problem: There are users, there are things. Each thing was made by one user, but should be accessible to a subset of all users (specified within the thing). Leaving the specification of...

Integration testing Rails API with basic authentication

I'm trying to get a test signing in using basic authentication. I've tried a few approaches. See code below for a list of failed attempts and code. Is there anything obvious I'm doing wrong. Thanks class ClientApiTest < ActionController::IntegrationTest fixtures :all test "adding an entry" do # No access to @request #@requ...

retrieving multi-valued DN attributes using ActiveLdap

Some users in my LDAP Directory have several uids assigned as such: dn: uid=user1,ou=People,o=org uid: user1 uid: nick1 dn: uid=user2,ou=People,o=org uid: user2 uid: nick2 While trying to get uid for these users using ActiveLdap (like User.uid) I only get the first uid attr as it is DN attribute. Is it possible with ActiveLdap to ge...

Rake: Calling tasks with arguments in default task

I have been playing around with Rake and Albacore, to see if I can replace our existing MSBuild script that deploys software with something that isn't XML. I have a task that will change the debug value inside a web.config to false. The task takes the directory of the web.config as an argument, but I can't quite figure out the syntax n...

Is there any language which is just "perfect" for web scraping?

I have used 3 languages for Web Scraping - Ruby, PHP and Python and honestly none of them seems to perfect for the task. Ruby has an excellent mechanize and XML parsing library but the spreadsheet support is very poor. PHP has excellent spreadsheet and HTML parsing library but it does not have an equivalent of WWW:Mechanize. Python ...

How does Ruby's REXML or REXML::XPath take an element and access its children nodes?

If the following code in Ruby can print out "browser type" and "count" individually as 2 separate lists, can REXML select each entry and then print out entry['title'].text and entry['dxp:metric'].attr('value') /* not using exact syntax */ on the same line as one list? require 'rexml/document' include REXML xmlfile = File.new("data....

Refactoring with dynamically typed language

Ok, I'm not trying to start a flame war here, and I know the argument between static and dynamic languages has been hashed out many times, including here. But I have a very practical question that hopefully someone here can shed some light on. Sorry for the length, but this is not a simple question with probably not a simple answer. Rub...

How do I define a setter for a property whose name ends with a question mark?

I'm trying to add a property called "enabled?" to a model with both a getter and a setter. However, when I do the following: def enabled?= value # .. logic goes here .. end I get syntax error, unexpected '?', expecting '\n' or ';' What should I be doing instead? ...

Ruby - return an array in random order

What is the easiest way to return an array in random order in Ruby? Anything that is nice and short that can be used in an IRB session like [1,2,3,4,5].random() # or random_sort([1,2,3,4,5]) ...

Check if a string matches a regex and strip a part of it out for use.

I want to add an automatic embedding feature when a YouTube URL is used (http://www.youtube.com/watch?v=VID, where VID is the video's ID). For this, I need to check if the given URL, stored in the variable url, matches "/http:\/\/www\.youtube\.com\/watch\?v=([a-z0-9]+).*/i", then I need to strip out the VID to use it (e.g. put the VID i...