Class: Proc
- Defined in:
- opal/opal/corelib/proc.rb,
opal/opal/corelib/marshal/write_buffer.rb
Overview
helpers: slice, each_ivar backtick_javascript: true use_strict: true
Class Method Summary collapse
Instance Method Summary collapse
- #<<(other) ⇒ Object
- #>>(other) ⇒ Object
- #__marshal__(buffer) ⇒ Object
- #arity ⇒ Object
- #binding ⇒ Object
- #call(*args, &block) ⇒ Object (also: #===, #yield, #[])
- #curry(arity = undefined) ⇒ Object
- #dup ⇒ Object (also: #clone)
- #lambda? ⇒ Boolean
- #parameters(lambda: undefined) ⇒ Object
- #source_location ⇒ Object
- #to_proc ⇒ Object
Class Method Details
.new(&block) ⇒ Object
9 10 11 12 13 14 15 |
# File 'opal/opal/corelib/proc.rb', line 9 def self.new(&block) unless block ::Kernel.raise ::ArgumentError, 'tried to create a Proc object without a block' end block end |
Instance Method Details
#<<(other) ⇒ Object
66 67 68 69 70 71 |
# File 'opal/opal/corelib/proc.rb', line 66 def <<(other) ::Kernel.proc do |*args, &block| out = other.call(*args, &block) call(out) end end |
#>>(other) ⇒ Object
59 60 61 62 63 64 |
# File 'opal/opal/corelib/proc.rb', line 59 def >>(other) ::Kernel.proc do |*args, &block| out = call(*args, &block) other.call(out) end end |
#__marshal__(buffer) ⇒ Object
97 98 99 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 97 def __marshal__(buffer) ::Kernel.raise ::TypeError, "no _dump_data is defined for class #{self.class}" end |
#arity ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'opal/opal/corelib/proc.rb', line 83 def arity %x{ if (self.$$is_curried) { return -1; } else if (self.$$arity != null) { return self.$$arity; } else { return self.length; } } end |
#binding ⇒ Object
100 101 102 103 104 105 106 |
# File 'opal/opal/corelib/proc.rb', line 100 def binding `if (self.$$is_curried) { #{::Kernel.raise ::ArgumentError, "Can't create Binding"} }` if defined? ::Binding ::Binding.new(nil, [], `self.$$s`, source_location) end end |
#call(*args, &block) ⇒ Object Also known as: ===, yield, []
51 52 53 54 55 56 57 |
# File 'opal/opal/corelib/proc.rb', line 51 def call(*args, &block) %x{ if (block !== nil) self.$$p = block; if (self.$$is_lambda) return $call_lambda(self, args); return $call_proc(self, args); } end |
#curry(arity = undefined) ⇒ Object
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 170 171 172 173 174 175 176 |
# File 'opal/opal/corelib/proc.rb', line 137 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) { #{::Kernel.raise ::ArgumentError, "wrong number of arguments (#{`arity`} for #{`self.length`})"} } } function curried () { var args = $slice(arguments), length = args.length, result; if (length > arity && self.$$is_lambda && !self.$$is_curried) { #{::Kernel.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(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 |
#dup ⇒ Object Also known as: clone
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'opal/opal/corelib/proc.rb', line 178 def dup %x{ var original_proc = self.$$original_proc || self, proc = function () { return original_proc.apply(this, arguments); }; $each_ivar(self, function(prop) { proc[prop] = self[prop]; }); return proc; } end |
#lambda? ⇒ Boolean
77 78 79 80 81 |
# File 'opal/opal/corelib/proc.rb', line 77 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 |
#parameters(lambda: undefined) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'opal/opal/corelib/proc.rb', line 108 def parameters(lambda: undefined) %x{ if (self.$$is_curried) { return #{[[:rest]]}; } else if (self.$$parameters) { if (lambda == null ? self.$$is_lambda : 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_location ⇒ Object
95 96 97 98 |
# File 'opal/opal/corelib/proc.rb', line 95 def source_location `if (self.$$is_curried) { return nil; }` `self.$$source_location` || nil end |
#to_proc ⇒ Object
73 74 75 |
# File 'opal/opal/corelib/proc.rb', line 73 def to_proc self end |