Parses the given string into an abstract syntax tree, returning the root node of that tree.
RubyVM::AbstractSyntaxTree.parse("x = 1 + 2") # => #<RubyVM::AbstractSyntaxTree::Node:SCOPE@1:0-1:9>
If keep_script_lines: true
option is provided, the text of the parsed source is associated with nodes and is available via Node#script_lines
.
If keep_tokens: true
option is provided, Node#tokens
are populated.
SyntaxError
is raised if the given string is invalid syntax. To overwrite this behavior, error_tolerant: true
can be provided. In this case, the parser will produce a tree where expressions with syntax errors would be represented by Node
with type=:ERROR
.
root = RubyVM::AbstractSyntaxTree.parse("x = 1; p(x; y=2") # <internal:ast>:33:in `parse': syntax error, unexpected ';', expecting ')' (SyntaxError) # x = 1; p(x; y=2 # ^ root = RubyVM::AbstractSyntaxTree.parse("x = 1; p(x; y=2", error_tolerant: true) # (SCOPE@1:0-1:15 # tbl: [:x, :y] # args: nil # body: (BLOCK@1:0-1:15 (LASGN@1:0-1:5 :x (LIT@1:4-1:5 1)) (ERROR@1:7-1:11) (LASGN@1:12-1:15 :y (LIT@1:14-1:15 2)))) root.children.last.children # [(LASGN@1:0-1:5 :x (LIT@1:4-1:5 1)), # (ERROR@1:7-1:11), # (LASGN@1:12-1:15 :y (LIT@1:14-1:15 2))]
Note that parsing continues even after the errored expression.
If a block is given, it prints out each of the elements encountered. Block parameters are (in that order):
depth: The recursion depth, plus one with each constructed value being encountered (Integer
)
offset: Current byte offset (Integer
)
header length: Combined length in bytes of the Tag and Length headers. (Integer
)
length: The overall remaining length of the entire data (Integer
)
constructed: Whether this value is constructed or not (Boolean
)
tag_class: Current tag class (Symbol
)
tag: The current tag number (Integer
)
der = File.binread('asn1data.der') OpenSSL::ASN1.traverse(der) do | depth, offset, header_len, length, constructed, tag_class, tag| puts "Depth: #{depth} Offset: #{offset} Length: #{length}" puts "Header length: #{header_len} Tag: #{tag} Tag class: #{tag_class} Constructed: #{constructed}" end
Reads at most maxlen bytes from the stream. If buf is provided it must reference a string which will receive the data.
See IO#readpartial
for full details.
Reads a one-character string from the stream. Raises an EOFError
at end of file.
Start streaming using encoding
Returns whether the form contained multipart/form-data
Generate a TextArea element, as a String
.
name
is the name of the textarea. cols
is the number of columns and rows
is the number of rows in the display.
Alternatively, the attributes can be specified as a hash.
The body is provided by the passed-in no-argument block
textarea("name") # = textarea("NAME" => "name", "COLS" => 70, "ROWS" => 10) textarea("name", 40, 5) # = textarea("NAME" => "name", "COLS" => 40, "ROWS" => 5)
Returns an array of Range
objects that represent the value of field 'Range'
, or nil
if there is no such field; see Range request header:
req = Net::HTTP::Get.new(uri) req['Range'] = 'bytes=0-99,200-299,400-499' req.range # => [0..99, 200..299, 400..499] req.delete('Range') req.range # # => nil
returns a charset parameter in Content-Type field. It is downcased for canonicalization.
If charset parameter is not given but a block is given, the block is called and its result is returned. It can be used to guess charset.
If charset parameter and block is not given, nil is returned except text type. In that case, “utf-8” is returned as defined by RFC6838 4.2.1
Parses self
destructively and returns self
containing the rest arguments left unparsed.
Generates formatted random number from raw random bytes. See Random#rand
.
Displays the given statement
on the standard output (or equivalent).
Load extra data embed into binary format String
object.
Dispatch enter and leave events for ClassVariableTargetNode
nodes and continue walking the tree.