ruby

Streaming data from Sinatra/Rack application

I am trying to stream textual data (XML/JSON) from a Ruby (1.9.1p378) Sinatra (1.0) Rack (1.2.1) application. The suggested solutions (e.g. http://stackoverflow.com/questions/3027435/is-there-a-way-to-flush-html-to-the-wire-in-sinatra) do not seem to work - the server just blocks when I yield elements of some infinite stream (e.g. from %...

[RUBY] DRY way to assign hash values to an object

Hi, I'm looking for a elegant way to assign the value stored inside an Hash into a pre-existed object. Just to be clear, if I have an object, say obj with two attributes, say name and age, I want to assign this values coming from an hash without do something like: obj.name = hash[:name] obj.age = hash[:age] Thanks for your attention...

Most efficient way to compare 9 numeric values using Ruby?

I was recently at an interview and was asked to solve a problem similar to this one, how would you code this for efficiency, I have a feeling my answer was not the best way to do this: Nine nickels and a traditional balance are sitting in front of you. All nickels have the same weight except for one counterfeit, which is slightly heav...

Create hash using block (Ruby)

Can I create a Ruby Hash from a block? Something like this (although this specifically isn't working): foo = Hash.new do |f| f[:apple] = "red" f[:orange] = "orange" f[:grape] = "purple" end ...

What are the trade offs between ffi and mri c extensions in ruby?

The title says it all, what are the trade offs between ffi and mri c extensions in ruby? ...

Is there an IDLE-like editor for ruby on Windows or Ubuntu?

I'd like to introduce my young cousin to a bit of programming. Ideally ruby, as that's what I'm familiar with. However finding a suitable text editor is a real pain. All I need of the editor is the ability to type a few lines of code, press 'Run' and get some results (or not, as the case may be). The simpler the editor the better, I don'...

Cucumber parse speed

We have been using Cucumber for some time now, and now have over 200 scenarios. Our startup speed is getting very slow, which makes a big difference in our edit-test-commit cycle. The problem seems to be the parsing of the feature files. Is there a way we can speed this up? NOTE: We are using IronRuby, which has a known slow startup tim...

How can i execute 2 or more commands in the same ssh session ?

I have the following script: #!/usr/bin/env ruby require 'rubygems' require 'net/ssh' Net::SSH.start('host1', 'root', :password => "mypassword1") do |ssh| stdout = "" ssh.exec("cd /var/example/engines/") ssh.exec!( "pwd" ) do |channel, stream, data| stdout << data if stream == :stdout end puts stdout s...

Why Ruby on Rails' Enumerable shows count of 3 but ".each" prints out item 1 time only

I have a Enumerable object returned from Mongoid (MongoDB object mapper) using HAML: = @employees.count = @employees.class - @employees.each do |e| =h e.inspect the count shows 3 the class shows Enumerable::Enumerator But only 1 item is printed out the object is returned in the controller using @employees = Employee.limit...

How to change Base URL in Selenium

Hi there, I have been testing a twitter web-based application using Selenium RC in Ruby. What I want to accomplish is: Click "Connect with Twitter", pops up the twitter oauth page, type username and password, and click Allow button. However, when it connects with twitter, it directs to twitter oauth page which is different URL from the...

ruby variable (Array) assignment misunderstanding (with push method)

I have discovered a flaw in my understanding of ruby or programming theory or both. Look At This Code: #!/usr/bin/ruby -w @instance_ar = [1,2,3,4] local_ar = @instance_ar local_ar_2 = local_ar ### irrelevant_local_ar = [5,6,7,8] ### for i in irrelevant_local_ar local_ar_2.push(i) end count = 0 for i in local_ar_2 puts "local_a...

Having a Ruby block/command silently fail without a blank 'rescue' block

Say I want a call to be run, and if it fails, it's no big deal; the program can continue without a problem. (I know that this is generally bad practice, but imagine a hypothetical, quick one-off script, and not a large project) The way I've been taught to do this was: begin thing_to_try rescue # awkward blank rescue block end next...

Access Object Attributes using Symbols

I have this helper that I am building: def myhelper(object, attributes = []) attributes.each do |attr| object.attr end end I invoke this helper using: myhelper Person, [:title, :name] What I am trying to achieve is to print a list of attributes in Person dynamically but object.attr in myhelper method won't work. How can I ...

What's the 'Ruby way' to iterate over an array - from array[n] to array[n - 1]?

Say I have an array of size 5. I want to take an index (from 0-4) as input, and iterate through the array, starting at the supplied index. For example, if the index given was 3, I want to iterate like so: arr[3] arr[4] arr[0] arr[1] arr[2] I can think of plenty of ways to do this - but what's the Ruby way to do it? ...

how get all routes in my rails application?

Sorry for my english. can I get all routes in my rails application? I need an output like rake routes and put the result in an array. Is it possible?, how? Thanks! ...

Deploying a Ruby project.

I have a finished Ruby project that has the standard structure for a multiple file Ruby program: project/ lib/ # Files the driver program uses go here. bin/ # Driver program goes here. tests/ # Unit tests go here. What I want to be able to do is type in project into the command line from any directory and have my progra...

What is require_relative in Ruby?

What is the difference between require_relative and require in Ruby? ...

Which plugins/gems should I use to dynamically generate thumbnails on-the-fly in Rails 3?

So, the thing is. I'm building my first rails app which reads images from a directory and it's subdirs. Now, I want to generate dynamic thumbnails of those images. But I don't want to fill up that directory with the thumbnail images. I was thinking of caching these thumbs separately for each user in temporary directory. Oh, and, I would...

Get a member URL action with ActiveResource

I have a route in my application that is like this: /deployments/:id/logs.json It is used to retrieve logs for a specific deployment. On my client code, based in ActiveResource I have this: logs = Deployment.find(deployment.id).get(:logs, opts) Where opts is some parameters that I send via query string. The problem with this code...

Optimized "Multidimensional" Arrays in Ruby

From birth I've always been taught to avoid nested arrays like the plague for performance and internal data structure reasons. So I'm trying to find a good solution for optimized multidimensional data structures in Ruby. The typical solution would involve maybe using a 1D array and accessing each one by x*width + y. Ruby has the abili...