Class: Proc

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

Overview

helpers: slice

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(&block) ⇒ Object



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

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

  block
end

Instance Method Details

#<<(other) ⇒ Object



64
65
66
67
68
69
# File 'opal/opal/corelib/proc.rb', line 64

def <<(other)
  proc do |*args, &block|
    out = other.call(*args, &block)
    call(out)
  end
end

#>>(other) ⇒ Object



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

def >>(other)
  proc do |*args, &block|
    out = call(*args, &block)
    other.call(out)
  end
end

#__marshal__(buffer) ⇒ Object

Raises:



95
96
97
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 95

def __marshal__(buffer)
  raise TypeError, "no _dump_data is defined for class #{self.class}"
end

#arityObject



81
82
83
84
85
86
87
88
89
# File 'opal/opal/corelib/proc.rb', line 81

def arity
  %x{
    if (self.$$is_curried) {
      return -1;
    } else {
      return self.$$arity;
    }
  }
end

#bindingObject



96
97
98
99
# File 'opal/opal/corelib/proc.rb', line 96

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

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'opal/opal/corelib/proc.rb', line 15

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

    var result, $brk = self.$$brk;

    if ($brk) {
      try {
        if (self.$$is_lambda) {
          result = self.apply(null, args);
        }
        else {
          result = Opal.yieldX(self, args);
        }
      } catch (err) {
        if (err === $brk) {
          return $brk.$v
        }
        else {
          throw err
        }
      }
    }
    else {
      if (self.$$is_lambda) {
        result = self.apply(null, args);
      }
      else {
        result = Opal.yieldX(self, args);
      }
    }

    return result;
  }
end

#curry(arity = undefined) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'opal/opal/corelib/proc.rb', line 130

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



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'opal/opal/corelib/proc.rb', line 171

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:



75
76
77
78
79
# File 'opal/opal/corelib/proc.rb', line 75

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'opal/opal/corelib/proc.rb', line 101

def parameters
  %x{
    if (self.$$is_curried) {
      return #{[[:rest]]};
    } else if (self.$$parameters) {
      if (self.$$is_lambda) {
        return self.$$parameters;
      } else {
        var result = [], i, length;

        for (i = 0, length = self.$$parameters.length; i < length; i++) {
          var parameter = self.$$parameters[i];

          if (parameter[0] === 'req') {
            // required arguments always have name
            parameter = ['opt', parameter[1]];
          }

          result.push(parameter);
        }

        return result;
      }
    } else {
      return [];
    }
  }
end

#source_locationObject



91
92
93
94
# File 'opal/opal/corelib/proc.rb', line 91

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

#to_procObject



71
72
73
# File 'opal/opal/corelib/proc.rb', line 71

def to_proc
  self
end