Class: Proc
- Inherits:
-
Object
- Object
- Proc
- Defined in:
- lib/proc_source.rb
Overview
:nodoc:
Instance Attribute Summary collapse
Class Method Summary collapse
-
._load(str) ⇒ Object
Load from Marshal format.
-
.from_string(str) ⇒ Object
Create a Proc object from a string of Ruby code.
- .json_create(o) ⇒ Object
Instance Method Summary collapse
-
#_dump(limit) ⇒ Object
Dump to Marshal format.
- #file ⇒ Object
- #line ⇒ Object
- #source_descriptor ⇒ Object
-
#to_json(*args) ⇒ Object
Dump to JSON string.
Instance Attribute Details
#source ⇒ Object
201 202 203 |
# File 'lib/proc_source.rb', line 201 def source @source ||= ProcSource.find(*self.source_descriptor) end |
Class Method Details
._load(str) ⇒ Object
Load from Marshal format.
p = Proc.new { false }
Marshal.load Marshal.dump p
227 228 229 230 |
# File 'lib/proc_source.rb', line 227 def self._load(str) @source = Marshal.load(str) @source.to_proc end |
.from_string(str) ⇒ Object
Create a Proc object from a string of Ruby code. It’s assumed the string contains do; end or { }.
Proc.from_string("do; 2+2; end")
254 255 256 |
# File 'lib/proc_source.rb', line 254 def self.from_string(str) eval "Proc.new #{str}" end |
.json_create(o) ⇒ Object
241 242 243 244 245 246 247 |
# File 'lib/proc_source.rb', line 241 def self.json_create(o) s, file, min, max = o['data'] ps = ProcString.new s ps.file = file ps.lines = (min..max) ps.to_proc end |
Instance Method Details
#_dump(limit) ⇒ Object
Dump to Marshal format.
p = Proc.new { false }
Marshal.dump p
218 219 220 221 222 |
# File 'lib/proc_source.rb', line 218 def _dump(limit) raise "can't dump proc, #source is nil" if source.nil? str = Marshal.dump(source) str end |
#file ⇒ Object
210 211 212 213 |
# File 'lib/proc_source.rb', line 210 def file source_descriptor @file end |
#line ⇒ Object
205 206 207 208 |
# File 'lib/proc_source.rb', line 205 def line source_descriptor @line end |
#source_descriptor ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/proc_source.rb', line 189 def source_descriptor @file ||= nil @line ||= nil unless @file && @line if md = /^#<Proc:0x[0-9A-Fa-f]+@(.+):(\d+)(.+?)?>$/.match(inspect) @file, @line = md.captures end end @line = @line.to_i [@file, @line] end |
#to_json(*args) ⇒ Object
Dump to JSON string
233 234 235 236 237 238 239 |
# File 'lib/proc_source.rb', line 233 def to_json(*args) raise "can't serialize proc, #source is nil" if source.nil? { 'json_class' => self.class.name, 'data' => [source.to_s, source.file, source.lines.min, source.lines.max] }.to_json#(*args) end |