class Alda::REPL

An instance of this class is an REPL session.

It provides an Alda::REPL::TempScore for you to operate on. To see what methods you can call in an REPL session, see instance methods of Alda::REPL::TempScore.

The session uses “> ” to indicate your input. Your input should be ruby codes, and the codes will be sent to an Alda::REPL::TempScore and executed.

After executing the ruby codes, if the score is not empty, it is played, and the translated alda codes are printed.

Note that every time your ruby codes input is executed, the score is cleared beforehand. To check the result of your previous input, run puts history.

Unlike IRB, this REPL does not print the result of the executed codes. Use p if you want.

Interrupt and SystemExit exceptions are rescued and will not cause the process terminating. exit terminates the REPL session instead of the process.

To start an REPL session in a ruby program, use Alda::repl. To start an REPL session conveniently from command line, run command ruby -ralda-rb -e "Alda.repl".

$ ruby -ralda-rb -e "Alda.repl"
> puts status
[27713] Server up (2/2 workers available, backend port: 33245)
> piano_ c d e f
[piano: c d e f]
> 5.times do
> c
> end
c c c c c
> puts history
[piano: c d e f]
c c c c c
> play
> save 'temp.alda'
> puts `cat temp.alda`
[piano: c d e f]
c c c c c
> system 'rm temp.alda'
> exit

Attributes

history[R]

The history.

Public Class Methods

new() → Alda::REPL click to toggle source

Creates a new Alda::REPL.

# File lib/alda-rb/repl.rb, line 125
def initialize
        @score = TempScore.new self
        @binding = @score.get_binding
        @lex = RubyLex.new
        @history = StringIO.new
end

Public Instance Methods

clear_history() → nil click to toggle source

Clears history.

# File lib/alda-rb/repl.rb, line 241
def clear_history
        @history = StringIO.new
        nil
end
play_score(code) → nil click to toggle source

Plays the score by sending code to command line alda.

# File lib/alda-rb/repl.rb, line 219
def play_score code
        try_command do
                Alda.play code: code, history: @history
                @history.puts code
        end
end
process_rb_code(code) → true or false click to toggle source

Processes the Ruby codes read. Sends it to a score and sends the result to command line alda. Returns false for breaking the REPL main loop, true otherwise.

# File lib/alda-rb/repl.rb, line 181
def process_rb_code code
        @score.clear
        begin
                @binding.eval code
        rescue StandardError, ScriptError => e
                $stderr.print e.full_message
                return true
        rescue Interrupt
                return true
        rescue SystemExit
                return false
        end
        code = @score.events_alda_codes
        unless code.empty?
                $stdout.puts code.yellow
                play_score code
        end
        true
end
rb_code() → String click to toggle source

Reads and returns the next Ruby codes input in the REPL session. It can intelligently continue reading if the code is not complete yet.

# File lib/alda-rb/repl.rb, line 160
def rb_code
        result = ''
        begin
                buf = Readline.readline '> '.green, true
                return unless buf
                result.concat buf, ?\n
                ltype, indent, continue, block_open = @lex.check_state result
        rescue Interrupt
                $stdout.puts
                retry
        end while ltype || indent.nonzero? || continue || block_open
        result
end
run() → nil click to toggle source

Runs the session. Includes the start, the main loop, and the termination.

# File lib/alda-rb/repl.rb, line 138
def run
        start
        while code = rb_code
                break unless process_rb_code code
        end
        terminate
end
start() → nil click to toggle source

Starts the session. Currently does nothing.

# File lib/alda-rb/repl.rb, line 151
def start
end
terminate() → nil click to toggle source

Terminates the REPL session. Currently just clears history.

# File lib/alda-rb/repl.rb, line 232
def terminate
        clear_history
end
try_command() { ... } → obj click to toggle source

Tries to run the block and rescue Alda::CommandLineError.

# File lib/alda-rb/repl.rb, line 206
def try_command
        begin
                yield
        rescue Alda::CommandLineError => e
                puts e.message.red
        end
end