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 %...
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...
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...
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
...
The title says it all, what are the trade offs between ffi and mri c extensions in ruby?
...
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'...
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...
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...
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...
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...
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...
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...
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 ...
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?
...
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!
...
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 the difference between require_relative and require in Ruby?
...
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...
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...
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...