Examples

rifraf edited this page Sep 14, 2010 · 3 revisions

The ExampleApp project contains a number of sample Ruby programs that act as example of what can be done.

If you run the Example app, it will run each one in turn. The code to run each example couldn’t be simpler…

  try {
    EmbeddedRuby er = new EmbeddedRuby();
    er.Mount("Applications");
    exitcode = er.Run("bottles.rb");
  } catch (Exception e) {
    Console.WriteLine(e.Message);
  }

bottles.rb

This is always a good proof of concept after Hello World.

10.times { |i|
  puts "#{i} green bottles"
}

gives:

0 green bottles
1 green bottles
2 green bottles
3 green bottles
4 green bottles
5 green bottles
6 green bottles
7 green bottles
8 green bottles
9 green bottles

list_args.rb

This simple application just shows that $0 (the running program), FILE (the current file) and ARGV (any command-line arguments) are set correctly. The output will be something like this:

$0 is list_args.rb
__FILE__ is list_args.rb
There are 3 command-line arguments  (ARGV)
0: 1
1: 2
2: 3

file_accesses.rb

This script is used to check that (selected) File operations can be done on embedded resource files. (Work in progress)

test_rexml.rb

This script shows REXML working by replicating some of the sample code from the REXML tutorial

test_unit_app.rb

This app shows trivial tests using test::unit. One passes, one fails:

Loaded suite test_unit_app
Started
F.
Finished in 0.102 seconds.

  1) Failure:
test_should_fail(TestDemo) [test_unit_app.rb:13]:
<5> expected but was
<4>.

2 tests, 2 assertions, 1 failures, 0 errors
exit

test_mocking_app.rb

This app shows trivial tests using flexmock and test::unit. One passes, one fails:

Loaded suite test_mocking_app
Started
.F
Finished in 0.132 seconds.

  1) Failure:
test_dog_wags_not(TestDog) [test_mocking_app.rb:14]:
<:happy> expected but was
<:sad>.

2 tests, 2 assertions, 1 failures, 0 errors
exit