Method: RinRuby#prompt

Defined in:
lib/rinruby.rb

#prompt(regular_prompt = "> ", continue_prompt = "+ ") ⇒ Object

When sending code to Ruby using an interactive prompt, this method will change the prompt to an R prompt. From the R prompt commands can be sent to R exactly as if the R program was actually running. When the user is ready to return to Ruby, then the command exit() will return the prompt to Ruby. This is the ideal situation for the explorative programmer who needs to run several lines of code in R, and see the results after each command. This is also an easy way to execute loops without the use of a here document. It should be noted that the prompt command does not work in a script, just Ruby’s interactive irb.

Parameters that can be passed to the prompt method:

  • regular_prompt: This defines the string used to denote the R prompt.

  • continue_prompt: This is the string used to denote R’s prompt for an incomplete statement (such as a multiple for loop).



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/rinruby.rb', line 300

def prompt(regular_prompt="> ", continue_prompt="+ ")
  raise "The 'prompt' method only available in 'interactive' mode" if ! @interactive
  return false if ! eval("0",false)
  prompt = regular_prompt
  while true
    cmds = []
    while true
      if @readline && @interactive
        cmd = Readline.readline(prompt,true)
      else
        print prompt
        $stdout.flush
        cmd = gets.strip
      end
      cmds << cmd
      begin
        if complete?(cmds.join("\n"))
          prompt = regular_prompt
          break
        else
          prompt = continue_prompt
        end
      rescue
        puts "Parse error"
        prompt = regular_prompt
        cmds = []
        break
      end
    end
    next if cmds.length == 0
    break if cmds.length == 1 && cmds[0] == "exit()"
    break if ! eval(cmds.join("\n"),true)
  end
  true
end