Class: Proc

Inherits:
Object show all
Defined in:
opal/opal/corelib/proc.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(&block) ⇒ Object



5
6
7
8
9
10
11
# File 'opal/opal/corelib/proc.rb', line 5

def self.new(&block)
  unless block
    raise ArgumentError, "tried to create a Proc object without a block"
  end

  block
end

Instance Method Details

#arityObject

FIXME: this should support the various splats and optional arguments



51
52
53
54
55
# File 'opal/opal/corelib/proc.rb', line 51

def arity
  `if (self.$$is_curried) { return -1; }`
  `if (self.$$arity) { return self.$$arity }`
  `self.length`
end

#bindingObject



62
63
64
65
# File 'opal/opal/corelib/proc.rb', line 62

def binding
  `if (self.$$is_curried) { #{raise ArgumentError, "Can't create Binding"} }`
  nil
end

#call(*args, &block) ⇒ Object Also known as: [], ===, yield



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'opal/opal/corelib/proc.rb', line 13

def call(*args, &block)
  %x{
    if (block !== nil) {
      self.$$p = block;
    }

    var result;

    if (self.$$is_lambda) {
      result = self.apply(null, args);
    }
    else {
      result = Opal.yieldX(self, args);
    }

    if (result === $breaker) {
      return $breaker.$v;
    }

    return result;
  }
end

#curry(arity = undefined) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'opal/opal/corelib/proc.rb', line 72

def curry(arity = undefined)
  %x{
    if (arity === undefined) {
      arity = self.length;
    }
    else {
      arity = #{Opal.coerce_to!(arity, Integer, :to_int)};
      if (self.$$is_lambda && arity !== self.length) {
        #{raise ArgumentError, "wrong number of arguments (#{`arity`} for #{`self.length`})"}
      }
    }

    function curried () {
      var args = $slice.call(arguments),
          length = args.length,
          result;

      if (length > arity && self.$$is_lambda && !self.$$is_curried) {
        #{raise ArgumentError, "wrong number of arguments (#{`length`} for #{`arity`})"}
      }

      if (length >= arity) {
        return self.$call.apply(self, args);
      }

      result = function () {
        return curried.apply(null,
          args.concat($slice.call(arguments)));
      }
      result.$$is_lambda = self.$$is_lambda;
      result.$$is_curried = true;

      return result;
    };

    curried.$$is_lambda = self.$$is_lambda;
    curried.$$is_curried = true;
    return curried;
  }
end

#dupObject Also known as: clone



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'opal/opal/corelib/proc.rb', line 113

def dup
  %x{
    var original_proc = self.$$original_proc || self,
        proc = function () {
          return original_proc.apply(this, arguments);
        };

    for (var prop in self) {
      if (self.hasOwnProperty(prop)) {
        proc[prop] = self[prop];
      }
    }

    return proc;
  }
end

#lambda?Boolean

Returns:



44
45
46
47
48
# File 'opal/opal/corelib/proc.rb', line 44

def lambda?
  # This method should tell the user if the proc tricks are unavailable,
  # (see Proc#lambda? on ruby docs to find out more).
  `!!self.$$is_lambda`
end

#parametersObject



67
68
69
70
# File 'opal/opal/corelib/proc.rb', line 67

def parameters
  `if (self.$$is_curried) { return #{[[:rest]]}; }`
  nil
end

#source_locationObject



57
58
59
60
# File 'opal/opal/corelib/proc.rb', line 57

def source_location
  `if (self.$$is_curried) { return nil; }`
  nil
end

#to_procObject



40
41
42
# File 'opal/opal/corelib/proc.rb', line 40

def to_proc
  self
end