sample/eval.rb


DEFINITIONS

This source file includes following functions.


   1  line = ''
   2  indent=0
   3  $stdout.sync = TRUE
   4  print "ruby> "
   5  while TRUE
   6    l = gets
   7    unless l
   8      break if line == ''
   9    else
  10      line = line + l 
  11      if l =~ /,\s*$/
  12        print "ruby| "
  13        next
  14      end
  15      if l =~ /^\s*(class|module|def|if|unless|case|while|until|for|begin)\b[^_]/
  16        indent += 1
  17      end
  18      if l =~ /^\s*end\b[^_]/
  19        indent -= 1
  20      end
  21      if l =~ /\{\s*(\|.*\|)?\s*$/
  22        indent += 1
  23      end
  24      if l =~ /^\s*\}/
  25        indent -= 1
  26      end
  27      if indent > 0
  28        print "ruby| "
  29        next
  30      end
  31    end
  32    begin
  33      print eval(line).inspect, "\n"
  34    rescue ScriptError, StandardError
  35      $! = 'exception raised' unless $!
  36      print "ERR: ", $!, "\n"
  37    end
  38    break if not l
  39    line = ''
  40    print "ruby> "
  41  end
  42  print "\n"