Created
February 12, 2012 02:16
-
-
Save jyotty/1805819 to your computer and use it in GitHub Desktop.
Doing ruby koans again as a refresher, getting a little impatient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require File.expand_path(File.dirname(__FILE__) + '/edgecase') | |
| class AboutControlStatements < EdgeCase::Koan | |
| # … | |
| def test_while_statement | |
| i = 1 | |
| result = 1 | |
| while i <= 10 | |
| result = result * i | |
| i += 1 | |
| end | |
| assert_equal (1..10).inject {|a,b| a*=b}, result | |
| end | |
| def test_break_statement | |
| i = 1 | |
| result = 1 | |
| while true | |
| break unless i <= 10 | |
| result = result * i | |
| i += 1 | |
| end | |
| assert_equal Math.gamma(11).to_int, result | |
| end | |
| def test_break_statement_returns_values | |
| i = 1 | |
| result = while i <= 10 | |
| break i if i % 2 == 0 | |
| i += 1 | |
| end | |
| assert_equal 2, result | |
| end | |
| def test_next_statement | |
| i = 0 | |
| result = [] | |
| while i < 10 | |
| i += 1 | |
| next if (i % 2) == 0 | |
| result << i | |
| end | |
| assert_equal [*(1..10).step(2)], result | |
| end | |
| # … | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment