Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
\ ==============================================================================
\ 
\ 	RubyFORTH -- Copyright (C) 2007-8, Marc Simpson (GPL). 
\ 
\	This file contains Forth code demonstrating parts of RubyFORTH.
\ 
\ ==============================================================================

\ --[ Generic Words ]-----------------------------------------------------------

: .a cr ." a" ;
: .b cr ." b" ;
: .c cr ." c" ;

\ --[ Looping tests ]-----------------------------------------------------------

: go1 10 0 do cr ." ==> Step..."    loop ;
: go2 10 0 do cr ." ==> Step: " i . loop ;

\ --[ Conditional Tests ]-------------------------------------------------------

: go3 if .a else .b then .c ;

\ --[ Mixed tests ]-------------------------------------------------------------

: go4 10 0 do i 2 / go3 loop ;

\ --[ Recursion ]---------------------------------------------------------------

( We have no need for a RECURSE word since : writes its header immediately...  )

: factorial ( n -- ) dup 1 > if dup 1- factorial * then ;

( Note that in Forths with RECURSE, we can do the following:                   )
(                                                                              )
(   : test ." hello" ;                                                         )
(   : test test space ." world" ;                                              )
(                                                                              )
( When test is run, 'hello world' prints.  Our model is different, requiring:  )
(                                                                              )
(   : test ." hello" ;                                                         )
(   ' test : test [ , ] space ." world" ;                                      )
(                                                                              )
( Equally, we could create an alias for the older word which we're seeking to  )
( overwrite;                                                                   )
(                                                                              )
(   : test ." hello" ;                                                         )
(   alias old-test test                                                        )
(   : test old-test space ." world" ;                                          )

\ --[ Deferral ]----------------------------------------------------------------

defer .greeting
: .formal   ." Good day, sir." ;
: .friendly ." Hey man." ;

: formal   ['] .formal [is] .greeting ;
: friendly ['] .friendly [is] .greeting ;

\ --[ Aliasing ]----------------------------------------------------------------

( Create shorthands for our context-switching words )

alias comp compiler
alias terp forth

\ --[ Files ]-------------------------------------------------------------------

: open: open constant ;

: .file ( fd -- )
    >r  r@ 0 seek
    begin r@ eof? not while r@ readline type repeat r> drop ;

( " README" r/o open: readme )
( readme .file )

\ --[ Primitives ]--------------------------------------------------------------

( We can create new Forth words that execute Ruby code by using 'prim' )

prim pwd push(Dir.pwd)
: .pwd pwd . ;
( .pwd )

prim (ls) system("ls " + pop)
: ls 0 parse (ls) ;
( ls -l )

\ ------------------------------------------------------------------------------