ruby

including spaces while using %w

The following code produces the output "xyz" a = %w{x y z} print a.to_s Is there an option that can be added to the block to allow spaces to be added? For example, I thought that by changing the code to this I might be able to space-separate the elements to produce an output of "x y z" a = %w{"x " "y " "z "} print a.to_s Instead...

"Warm Up Cache" on deployment

I am wondering if anyone has any plugins or capistrano recipes that will "pre-heat" the page cache for a rails app by building all of the page cached html at the time the deployment is made, or locally before deployment happens. I have some mostly static sites that do not change much, and would run faster if the html was already written...

Problem With Regular Expression to Remove HTML Tags

In my Ruby app, I've used the following method and regular expression to remove all HTML tags from a string: str.gsub(/<\/?[^>]*>/,"") This regular expression did just about all I was expecting it to, except it caused all quotation marks to be transformed into &#8220; and all single quotes to be changed to &#8221; . What's the obviou...

What's with Ruby's ZipInputStream screwing up my line endings?

I'd be happy with ZipInputStream taking indecent liberties with the line endings that are stored in a file if it would at least get them right for the platform I'm storing the file on. Unfortunately, I pull a text file (.txt, .cpp. .etc.) out of a zip and the \n (0x0A) gets replaced with a \r\n (0x0d0a) and, as you can imagine, this is ...

Sinatra success stories

Have you used Sinatra successfully? What kind of a project was it? In what situations would you recommend using Sinatra instead of Rails or Merb? ...

Calling MS .Net Webservices with Ruby

Has anyone done this / have some example code? ...

Create web application with ajax from the begining or add ajax later?

I'm working on my first Ruby on Rails aplication and it's quite big (at least for me ;) - database has about 25 tables). I'm still learning Ruby and Rails and I never wrote anything in Javascript nor Ajax. Should I add Ajax to my application from the begining? Or maybe it will be better to add it latter? Or in the other words: is it (r...

When you say Ruby is reflective, does this mainly refer to "duck typing"?

I was reading a text describing Ruby and it said the following: Ruby is considered a “reflective” language because it’s possible for a Ruby program to analyze itself (in terms of its make-up), make adjustments to the way it works, and even overwrite its own code with other code. I'm confused by this term 'reflective' - ...

In Ruby, how do I replace the question mark character in a string?

In Ruby, I have: require 'uri' foo = "et tu, brutus?" bar = URI.encode(foo) # => "et%20tu,%20brutus?" I'm trying to get bar to equal "et%20tu,%20brutus%3f" ("?" replaced with "%3F") When I try to add this: bar["?"] = "%3f" the "?" matches everything, and I get => "%3f" I've tried bar["\?"] bar['?'] bar["/[?]"] bar["/[\?]"...

Rails nested resources

Here's the routes.rb: map.resources :assignments, :shallow => true do |assignment| assignment.resources :problems end How do i get the url to edit a problem (/assignments/xyz/problems/abc/edit), in code? I have tried both edit_assignment_problem_path(assignment,problem) and edit_problem_path(problem). While the firs...

Regex matching question

Alright, i promised myself i would learn Regex one day.. but today is not that day. What is the correct expression for matching #_ (where _ is any character EXCEPT {)? Clarification: I am working on a syntax highlighting system for Ruby and i am defining the rules for comments. The specification that the '{' not be included is to d...

What Ruby on Rails image library should I use for making vocab cards?

I am making a flashcard builder in Ruby on Rails. What image libraries are recommended for Ruby on Rails? I've used ImageMagick and I've had memory issues on the servers. I hear ImageScience is good but I don't know if I can scale images and draw words on the images (a la lolcats style.) ...

[ruby] How to convert STDIN contents to an array?

I've got a file INPUT that has the following contents: 123\n 456\n 789 I want to run my script like so: script.rb < INPUT and have it convert the contents of the INPUT file to an array, splitting on the new line character. So, I'd having something like myArray = [123,456,789]. Here's what I've tried to do and am not having much luck: ...

Ruby only creating 3 threads at a time

I am trying to run 500 clients that send some request to the server simultaneously for load testing purpose. The client is a ruby program again. Sounds trivial. But I am facing weird problem with ruby threads. My code looks like this - n = 10 n.times do Thread.new do `calc` end end The code is a sample. I'm just trying to ru...

Shoes and Gems

Code: Shoes.setup do gem 'mechanize' end require 'rubygems' require 'mechanize' Running Shoes on it says: no such file to load -- mechanize Thank you. ...

Netbeans occasional non-compliation using Ruby MRI - solution?

Quite frequently when I save changes to a Ruby file in Netbeans and try to run the file, the interpreter simply doesn't do anything. The only way around it is to temporarily switch to the JRuby interpreter and then switch back to the default Ruby interpreter (1.86). Besides this hack, is anyone aware of a fix for this problem? It has pre...

Writing Module for Ruby

How do you write a module for ruby. in python you can use # module.py def helloworld(name): print "Hello, %s" % name # main.py import module module.helloworld("Jim") Back to the Question how do you create a module in/for ruby ...

High-performance RSS/Atom parsing with Ruby on Rails

I need to parse thousands of feeds and performance is an essential requirement. Do you have any suggestions? Thanks in advance! ...

Polymorphism across C++ and Ruby using SWIG

I use SWIG to wrap a Ruby script around a C++ library. In Ruby, I can inherit from a C++ class, but I cannot pass the resulting pointer to a C++ function in a polymorphic way. Here is a concrete example. The SWIG interface file defines base class Animal with virtual function sound(): [animals.i] %module(directors="1") animals %{ #i...

Search Websites Content

How do you search a Websites source code with ruby, hard to explain but heres the code for doing it in python import urllib2, re word = "How to ask" source = urllib2.urlopen("http://stackoverflow.com").read() if re.search(word,source): print "Found it "+word ...