background preloader

Ruby

Facebook Twitter

Iterate over a cartesian product. Handy Gandy (Guest) on 2010-09-14 15:55 I have an array of N arrays A_i: [A_1,A_2,... ,A_N]. I want to iterate over the cartesian product A_1xA_2...xA_N. Is there a simple way of doing this without specifying N? Andrew Wagner (Guest) on 2010-09-14 16:11 This thread seems to be a pretty thorough treatment of the idea: . Jörg W Mittag (Guest) on 2010-09-14 17:15 Handy Gandy wrote: > I have an array of N arrays A_i: [A_1,A_2,... on 2010-09-14 19:33 On Tue, 14 Sep 20 10 17:13:11 +0200, Jörg W Mittag wrote: > # => [1, 3, 6], [1, 3, 7], [1, 3, 8], [1, 3, 9], # => [1, 4, 6],> [1, 4, 7], [1, 4, 8], [1, 4, 9], # => [1, 5, 6], [1, 5, 7], [1, 5,> 8], [1, 5, 9], # => [2, 3, 6], [2, 3, 7], [2, 3, 8], [2, 3, 9], #> => [2, 4, 6], [2, 4, 7], [2, 4, 8], [2, 4, 9], # => [2, 5, 6],> [2, 5, 7], [2, 5, 8], [2, 5, 9] # => ]>> jwm That looks so cool I wish I knew what it does. Adam Prescott (Guest) on 2010-09-14 22:41 on 2010-09-14 22:44 Robert Klemme (Guest) on 2010-09-15 10:44.

Ruby Programming/Unit testing - Wikibooks, collection of open-content textbooks. Unit testing is a great way to catch errors early in the development process, if you dedicate time to writing appropriate and useful tests. As in other languages, Ruby provides a framework in its standard library for setting up, organizing, and running tests called Test::Unit. There are other very popular testing frameworks, rspec and cucumber come to mind. Specifically, Test::Unit provides three basic functionalities: A way to define basic pass/fail tests.A way to gather like tests together and run them as a group.Tools for running single tests or whole groups of tests. A Simple Introduction[edit] First create a new class. # File: simple_number.rb class SimpleNumber def initialize(num) raise unless num.is_a?

Let's start with an example to test the SimpleNumber class. Which produces >> ruby tc_simple_number.rb Loaded suite tc_simple_number Started . So what happened here? Let's try a more complicated example. >> ruby tc_simple_number2.rb Loaded suite tc_simple_number2 Started F.. Exercises[edit] Ruby Best Practices - Reading Ruby's Standard Library for Fun and Profit. Note: This post is inspired by JEG2’s excellent code reading talk at MWRC 2009, called LittleBIGRuby. Go watch it if you have time, then come back and read this. You might also want to check out the Question 5 Ways interview at Pat Eyler’s “On Ruby” blog for more code reading goodness. We all need to hunt bugs and we all need to integrate our code with other systems. Some of us need to make use of undocumented libraries, and others need to examine code to perform security audits.

Almost a decade ago, Joel Spolsky would have told you it’s harder to read code than it is to write it , and that was probably true at one point. Beginners and even intermediate developers may need a lot of hand holding at first, that’s only to be expected. But where should you start? How I Read Code I’m going to walk through the process I generally follow when I’m reading code for the purpose of understanding its implementation. Start with a simple example. = Greg's Transcript = Me: Hi Jia: Hello Me: Fun, huh? Stupid Ruby Tricks. Ruby Readline. What is Readline? The GNU Readline Library’s website sums it up best: The GNU Readline library provides a set of functions for use by applications that allow users to edit command lines as they are typed in. (…) The Readline library includes additional functions to maintain a list of previously-entered command lines, to recall and perhaps reedit those lines, and perform csh-like history expansion on previous commands.

Users that have worked with the shell are very familiar with readline. This is the library that provides ⌃A, ⌃E, ↑, ↓, and a number of other keyboard shortcuts that you probably expect to have when working in a shell. Ruby ships with support for working with readline (or libedit). You just need to include the Readline module: It takes just a few lines of Ruby to get the basic functionality and the core advantages that Readline provides such as keyboard shortcuts and history. Basic Functionality Here is the standard Ruby readline sample program: The Readline Module Ignore SIGINT: Bundler: The best way to manage Ruby applications.

Katz Got Your Tongue? RubyGems.org | your community gem host. Learning Ruby with the EdgeCase Ruby Koans. Ruby Programming Language. Rdoc.