Iterate over a cartesian product. 2010/9/15 Jörg W Mittag removed_email_address@domain.invalid: This looks nice. How did you do the formatting? By hand 14:09:29 ~$ gem19 install ‘By hand’ ERROR: could not find gem By hand locally or in a repository 14:09:58 ~$ Hm… => nil That’s nice. Unfortunately there is no #slice which has two return values. A,b = arr.slice 0,1 But we can do this to achieve the same (i.e. non destructively partitioning): irb(main):016:0> a=(1…5).to_a => [1, 2, 3, 4, 5] irb(main):017:0> y=(x=a.dup).slice! Well, I guess the simplest approach is still irb(main):001:0> a=(1…5).to_a => [1, 2, 3, 4, 5] irb(main):002:0> x,*y = a => [1, 2, 3, 4, 5] irb(main):003:0> x => 1 irb(main):004:0> y => [2, 3, 4, 5] So we can do Kind regards robert. 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.