ruby

Automatically encode Rack output with JSON when Content-Type is application/json

I've got a modular Sinatra app and I'd like to encode the output as JSON when the content-type dictates. At the moment I'm doing so manually in my routes: get 'someroute' do # content-type is actually set with a before filter # included only for clarity content_type 'application/json', :charset => 'utf-8' # .. # {:su...

Trying to understand Threading in Ruby. Test Code doesn't work as expected, why?

So, I wrote a quick thread example for myself, using the ruby docs for thread: puts "Hello World" test = Thread.new do while true puts Time.now end end puts "Goodbye World" I would EXPECT this code to run forever, printing "Hello World", a few time stamps, goodbye world, and then a whole bunch of time stamps, until I manually break ...

Ruby on Rails: short hand to put commas in between elements in an array?

users_allowed_to_be_viewed.map {|u| "#{u.id},"} but that gives 1,2,3, What would be a short way to just get something like 1,2,3 ...

Unload a ruby class

I have the following in the file a.rb: require foo and i need to unload foo, to load the foo from b.rb, c.rb and other files. How i can do? ...

Share instance data across objects/classes in a custom web app

I'm working on a rack web app from the ground up in Ruby and I want to be able to share the instances of several objects across all classes in the app. For example, the logger, the request information, a currently authenticated user, get/post parameters, etc. To any of you familiar with PHP, I've accomplished this in the past with stati...

Ruby on Rails: how do I check if a variable in an instance of a class?

In Java, you can do instanceof. Is there a Ruby equivalent? ...

A Faster / More Scalable Approach to Twitter OAuth Dance in Rails?

I'm running a Rails app on the Heroku Stack (complete with Memcached, DJ Asynchronous workers, MongoDB persistent storage). Right now we use Twitter Oauth as the only authentication option on our site. (We plan to branch out to FB connect, OpenID, and/or Email/password eventually). Ruby/Rails apps, as you probably know, don't support c...

Extracting a Hostname's TLD with a Regular Expression

Extracting an accurate representation of the top-level domain of a hostname is complicated by the fact that each top-level domain registry is free to make up its own policies regarding how domains are issued and what subdomains are defined. As there doesn't appear to be any standards body coordinating these or establishing standards, thi...

Searching Binary Data in Ruby

Using only pure ruby (or justifiably commonplace gems) is there an efficient way to search a large binary document for a specific string of bytes? Deeper context: the mpeg4 container format is a 4-byte indexed serialised data structure, without having to parse the structure fully (I can assume it is valid) I want to pull out specific ...

How to run Ruby and GIT commands in one place on Windows

I have Ruby and GIT installed on my windows box. To run GIT commands I am utilizing the GIT Bash. To run Ruby commands I am using the command line. I have not been successful running GIT commands from the CMD line nor can I seem to run Ruby commands from inside the GIT Bash. I would love to be able to run commands for both GIT and Ruby ...

Create a case-insensitive regular expression from a string in Ruby

Let's say that I have an arbitrary string like `A man + a plan * a canal : Panama!` and I want to do a regex search for strings that are the same other than case. That is, this regular expression should match the string `a man + A PLAN * a canal : PaNaMa!` I take it the best approach is to backslash-escape every character with a sp...

Dashes in XML Builder

Is there a way to generate xml tags with dashes in builer? Imagine I want to generate the following XML: <ninja-programmer> Jon Skeet </ninja-programmer> I am not able to do it like: require 'builder' data = '' x = Builder::XmlMarkup.new(:target => data, :indent => 2) x.instruct! x.ninja-programmer "Jon Skeet" That would confu...

How does one comment in an erb template?

I have some trivial markup that looks like the following: <li class="someclass"> <=% t'model.attr' %> </li> Is there a trivial way to comment that out? Just wrapping <!-- --> around the block will still leave the ruby code available to the template. This means I have to comment out the HTML and Ruby specific code separately. What's...

Why does my Ruby thread demo not use both cores?

Hopefully this screenshot will explain my question: a = Thread.new { loop {} } b = Thread.new { loop {} } a.join So how come both of my cores aren't maxed out? No matter how many threads I use, it's the same each time; the total CPU usage never seems to exceed 52%. >ruby -v ruby 1.8.6 (2010-02-04 patchlevel 398) [i386-mingw32] ...

rcov + Rails3 rc now broken from linecache dependency

I hit a wall today with rcov + Rails3. I am developing my Rails3 app using Ruby 1.9.2-preview3. rcov and relevance-rcov do not work with Ruby 1.9.2 yet. I can't find any fork of rcov that does yet either. It wasn't that big of a deal since I could easily switch over to Ruby 1.8.7 using rvm --default 1.8.7; rake test:coverage. So the...

Getting Textmate to recognize Ruby version upgrade

I used the instructions at http://bparanj.blogspot.com/2010/06/installing-ruby-191-on-snow-leopard.html to install Ruby version 1.92 on my Mac running Snow Leopard. The only deviation is in step 3, which calls for .bash_profile to be updated. I have .profile, but not .bash_profile, in my home directory, so I added the export command to...

Appropriate use of Grails, Rails, etc?

We've got an Excel spreadsheet floating around right now (globally) at my company to capture various pieces of information about each countries technology usage. The problem is that it goes out, gets changes, but they're never obvious, and often conflicting - and then we have to smash them together. To me, the workbook is no more than a ...

Ruby: How to cast from a class to another using ActiveRecord

Hi, I'm currently using activerecord STI and trying to cast an object type from A to B (same parent). Is there a standard way of achieving that? ...

JavaScript Array to String

I'm using the jQuery plugin jQuery-Tokenizing-Autocomplete in a rails application with a has_many association. I need to save the values to the database, but to do that I need it to be a string, not an array. So the result should equal "1","2","3". Hope this clarifies. The javascript used to generate this array: $.TokenList.ParseValue...

displaying flash messages

I am quite new to rails... I am having an issue with displaying of flash messages... Am displaying it in views as well as application.html.erb (layout file) in the view i just have flash[:notice] and in the application.html.erb I have flash.each do |name, msg| content_tag :div, msg, :id => "flash_#{name}" end I want to display...