Exemplo n.º 1
0
  @JRubyMethod(name = "initialize", frame = true, visibility = Visibility.PRIVATE)
  public IRubyObject initialize(ThreadContext context, Block procBlock) {
    if (!procBlock.isGiven()) {
      throw getRuntime().newArgumentError("tried to create Proc object without a block");
    }

    if (isLambda() && procBlock == null) {
      // TODO: warn "tried to create Proc object without a block"
    }

    block = procBlock.cloneBlock();

    if (isThread()) {
      // modify the block with a new backref/lastline-grabbing scope
      StaticScope oldScope = block.getBody().getStaticScope();
      StaticScope newScope =
          new BlockStaticScope(oldScope.getEnclosingScope(), oldScope.getVariables());
      newScope.setBackrefLastlineScope(true);
      newScope.setPreviousCRefScope(oldScope.getPreviousCRefScope());
      newScope.setModule(oldScope.getModule());
      block.getBody().setStaticScope(newScope);
    }

    block.type = type;
    block.setProcObject(this);

    file = context.getFile();
    line = context.getLine();
    return this;
  }
Exemplo n.º 2
0
  public static RubyModule getClassVariableBase(Ruby runtime, StaticScope scope) {
    RubyModule rubyClass = scope.getModule();
    while (rubyClass.isSingleton() || rubyClass == runtime.getDummy()) {
      // We ran out of scopes to check
      if (scope == null) return null;

      scope = scope.getPreviousCRefScope();
      rubyClass = scope.getModule();
      if (scope.getPreviousCRefScope() == null) {
        runtime
            .getWarnings()
            .warn(
                ID.CVAR_FROM_TOPLEVEL_SINGLETON_METHOD,
                "class variable access from toplevel singleton method");
      }
    }
    return rubyClass;
  }