Sha256: b9b88d92e3282e111a13b7a553322a567defe9e76c7b60ab0492f86770b577e6

Contents?: true

Size: 1.74 KB

Versions: 40

Compression:

Stored size: 1.74 KB

Contents

module Kernel

  # safe_eval(string [, binding [, filename [,lineno]]] *allowed_class/module_names )  -> obj
  #
  # Evaluates the Ruby expression(s) in <em>string</em>. If
  # <em>binding</em> is given, which must be a <code>Binding</code>
  # object, the evaluation is performed in its context. If the
  # optional <em>filename</em> and <em>lineno</em> parameters are
  # present, they will be used when reporting syntax errors.
  #
  #    def get_binding(str)
  #      return binding
  #    end
  #    str = "hello"
  #    safe_eval "str + ' Fred'" ,Kernel                      #=> "hello Fred"
  #    safe_eval "str + ' Fred'", get_binding("bye") ,Kernel  #=> "bye Fred"
  def safe_eval(*args)

    # defaults
    begin
      allowed=  Array.new
      eval_exception= String.new
      tmp_array= nil
    end

    # separate allowed names
    begin
      tmp_array= Array.new
      args.each do |argument|
        case argument.class.to_s.downcase

          when "class","module"
            begin
              allowed.push argument
            end

          else
            begin
              tmp_array.push argument
            end

        end
      end
      args= tmp_array
    end

    # build exception list to eval
    begin
      allowed.each do |one_name|
        eval_exception += "|"+one_name.to_s
      end
    end

    # trim un wanted elements from string
    begin
      args.each do |argument|
        case argument.class.to_s.downcase

          when "string"
            begin
              #TODO new regex!  # /(\b|\.|\{|\>)[A-Z]\w*/
              #args[args.index(argument)]= argument.gsub( /(\b|\.|\{|\>)[A-Z]\w*/ ,'')
            end

        end
      end
    end

    # do save eval
    begin
      eval(*args)
    end

  end if $DEBUG == true

end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
procemon-1.2.1 lib/procemon/function/meta/eval.rb
procemon-1.2.0 lib/procemon/function/meta/eval.rb
procemon-1.1.1 lib/procemon/function/meta/eval.rb
procemon-1.0.4 lib/procemon/function/meta/eval.rb
procemon-1.0.3 lib/procemon/function/meta/eval.rb
procemon-1.0.1 lib/procemon/function/meta/eval.rb
procemon-1.0.0 lib/procemon/function/meta/eval.rb
procemon-0.9.1 lib/procemon/function/meta/eval.rb
procemon-0.9.0 lib/procemon/function/meta/eval.rb
procemon-0.8.1 lib/procemon/function/meta/eval.rb
procemon-0.8.0 lib/procemon/function/meta/eval.rb
procemon-0.7.0 lib/procemon/function/meta/eval.rb
procemon-0.6.3 lib/procemon/function/meta/eval.rb
procemon-0.6.2 lib/procemon/function/meta/eval.rb
procemon-0.6.1 lib/procemon/function/meta/eval.rb
procemon-0.5.0 lib/procemon/function/meta/eval.rb
procemon-0.4.5 lib/procemon/function/meta/eval.rb
procemon-0.4.4 lib/procemon/function/meta/eval.rb
procemon-0.4.3 lib/procemon/function/meta/eval.rb
procemon-0.4.2 lib/procemon/function/meta/eval.rb