ruby

Webrat Mechanize outside of Rails

I'm trying to use Webrat in a standalone script to automate some web browsing. How do I get the assert_contain method to work? require 'rubygems' require 'webrat' include Webrat::Methods include Webrat::Matchers Webrat.configure do |config| config.mode = :mechanize end visit 'http://gmail.com' assert_contain 'Welcome to Gmail' I ...

Ruby class_eval and yield

Man, I'm peeling the layers of the onion today, anyway here's the code class MyClass def initialize(dynamic_methods) @arr = Array.new(dynamic_methods) @arr.each { |m| self.class.class_eval do define_method(m) do "<#{yield self if block_given?}>" end end } end end tmp = MyClass.ne...

How does Ruby's sort_by {rand} work?

I think this is a great Ruby one-liner: someArray.sort_by {rand} It's concise, it's readable, and it works - but I don't quite understand how. Here's what I know: rand evaluates to a number between 0 and 1 (like 0.783468632804653) rand is being repeatedly evaluated in the code above, because assigning it to x first breaks the random...

Opensource Online IDE

There're several online IDEs for PHP and some even for Python, but is there any open-source online IDE like IDEone that supports atleast the major languages (PHP, Python, Ruby etc..)? ...

How to do full stack integration testing of web applications

I'm looking to enhance our current test suites, and continuous integration builds with full stack integration/acceptance testing. I'm looking into tools like Culerity and Selenium that can execute front end javascript while running user stories. I'm looking for something that can provide coverage of front-end javascript and high level ...

How can I restrict Markdown syntax in Ruby?

I wish to implement Markdown in a Rails CMS comments system using a Ruby library such as Maraku or Kramdown. I need to restrict which Markdown features the users can submit. In this system users aren't allowed to insert to images, html, or perform any heavy editing, but emphasis and hyperlinks are okay. Essentially, I wish to create som...

How can I create a web service using Ruby on Rails?

How can I create a web service using Ruby on Rails? Please give an example. ...

How to refactor singleton methods in Ruby?

Currently I have code like the following (simplified somewhat). Eventually, I've added more and more new classes like D1/D2, and I think it's time to do some refactoring to make it more elegant. The goal of course is to make adding new class Dx use as little duplicate code as possible. At least, the duplicate parts of calling FileImporte...

Reading a file in Rails Template

I'm making a Rails template file as introduced below: http://asciicasts.com/episodes/148-app-templates-in-rails-2-3 I have a big file I want to make, so instead of doing: file "bigfile.txt", <<-EOF content of the file... EOF I want to read the content from another file. What I have now is: file "public/stylesheets/sass/960.sass...

Capture webcam's image with Ruby

Is there any library in Ruby which I can use to capture the webcam's image? Has to run on GNU/Linux. ...

RMagick filesize giving strange output for different sized images

When the snippet below surprisingly giving the same output for the original and the resized image. Executed with rmagick (2.12.2) and ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux] on Ubuntu Jaunty. img = nil File.open("~/rmagick/test/original.JPG", "r") { |f| img = f.read } img = Magick::Image::from_blob(img).first p img.filesize #...

Ruby how to reset class methods

Using module_eval, my code allows me to dynamically create and add new methods to a class based on input parameters. See this post for an example http://stackoverflow.com/questions/2039826/ruby-classeval-and-yield Now my question is how do I reset the class back to its original methods? Is there a simple method I can use to reset a clas...

Unit testing an OpenID server

Hello, I'm currently working on an authentication system derived of OpenID. I work in Ruby and I use the ruby-openid library. Currently, for my tests, I use fakeweb to fake an openid consumer and server and check everything works well. But I don't like the way I do it. And I'm sure there'd be a much better way to do so. So my question...

How to handle the event when the selected item changes in an HTML select

Hi, I currently have two Ruby selects: one for categories and the other one for subcategories. As you may anticipate, the second one has to update itself every time the first one changes. For example, if I select from the first one the category "Sports", the second select should load all the sports. How can I handle that "index change...

Scripting HTTP more effeciently

Often times I want to automate http queries. I currently use Java(and commons http client), but would probably prefer a scripting based approach. Something really quick and simple. Where I can set a header, go to a page and not worry about setting up the entire OO lifecycle, setting each header, calling up an html parser... I am looking ...

default to text in a search for with ruby and rails

I want to have a text value for a form like "search website.com..." but if the form has been submitted i want the query to appear- i dont know ruby idioms that well but i was think <%= text_field_tag "q", params[:q] | "search website.com...." %> is this correct? ...

Getting count of elements by `created_at` by day in a given month.

I wanted to make a simple chart of users that have been created in the past month in my app. Like basically for each day in the past month I want to show the count of users that have registered that day. What I have so far: # Controller @users = User.count(:order => 'DATE(created_at) DESC', :group => ["DATE(created_at)"]) # View <% @us...

DES3 decryption in Ruby on Rails

My RoR server receives a string, that was encrypted in C++ application using des3 with base64 encoding The cipher object is created so: cipher = OpenSSL::Cipher::Cipher::new("des3") cipher.key = key_str cipher.iv = iv_str key_str and iv_str: are string representations of key and initialization vector for encryption algorithm. They a...

Inline DTD with Builder

Hi, How would I create this... <!DOCTYPE root-element[ <!ENTITY % w3centities PUBLIC "-//W3C//ENTITIES Combined Set//EN//XML" "w3centities.ent" > %w3centities; ]> ...using Builder? I can manage everying apart from the "%w3centities;" in the second to last line with the following code: xml.declare! :DOCTYPE, "root-element"...

How to decode Rijndael in ruby (encoded in VB.net)

Hello, I am using Rinjael to encode in VB.NET and need to decode in Ruby. My VB.NET encryption class looks like this: Private Class Encryptor Private symmetricKey As System.Security.Cryptography.RijndaelManaged Private iVector As Byte() Private Key As Byte() Public Function encrypt(ByVal data As String) ...