Sha256: 117fbaa6231da3030d2aa70c7c071aac163a44690603122fceaeff1325ddb302

Contents?: true

Size: 1.7 KB

Versions: 6770

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Byebug
  module Helpers
    #
    # Utilities to assist command parsing
    #
    module ParseHelper
      #
      # Parses +str+ of command +cmd+ as an integer between +min+ and +max+.
      #
      # If either +min+ or +max+ is nil, that value has no bound.
      #
      # @todo Remove the `cmd` parameter. It has nothing to do with the method's
      # purpose.
      #
      def get_int(str, cmd, min = nil, max = nil)
        return nil, pr("parse.errors.int.not_number", cmd: cmd, str: str) unless /\A-?[0-9]+\z/.match?(str)

        int = str.to_i
        if min && int < min
          err = pr("parse.errors.int.too_low", cmd: cmd, str: str, min: min)
          return nil, err
        elsif max && int > max
          err = pr("parse.errors.int.too_high", cmd: cmd, str: str, max: max)
          return nil, err
        end

        int
      end

      #
      # @return true if code is syntactically correct for Ruby, false otherwise
      #
      def syntax_valid?(code)
        return true unless code

        without_stderr do
          begin
            RubyVM::InstructionSequence.compile(code)
            true
          rescue SyntaxError
            false
          end
        end
      end

      #
      # @return +str+ as an integer or 1 if +str+ is empty.
      #
      def parse_steps(str, cmd)
        return 1 unless str

        steps, err = get_int(str, cmd, 1)
        return nil, err unless steps

        steps
      end

      private

      #
      # Temporarily disable output to $stderr
      #
      def without_stderr
        old_stderr = $stderr
        $stderr = StringIO.new

        yield
      ensure
        $stderr = old_stderr
      end
    end
  end
end

Version data entries

6,770 entries across 6,767 versions & 30 rubygems

Version Path
ory-client-0.0.1.alpha37 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
ory-client-0.0.1.alpha36 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
ory-kratos-client-0.8.2.alpha1 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
ory-client-0.0.1.alpha31 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
primary_connect_proto-0.4.0 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
primary_connect_proto-0.3.0 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
primary_connect_proto-0.2.0 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
primary_connect_proto-0.1.0 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
ory-client-0.0.1.alpha30 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
ory-client-0.0.1.alpha29 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
primary_connect_proto-0.0.6 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
ory-client-0.0.1.alpha28 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
primary_connect_proto-0.0.5 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
ory-client-0.0.1.alpha27 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
ory-client-0.0.1.alpha24 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
primary_connect_proto-0.0.4 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
primary_connect_proto-0.0.3 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb
ory-client-0.0.1.alpha23 vendor/bundle/ruby/2.5.0/gems/byebug-11.1.3/lib/byebug/helpers/parse.rb