解析Ruby
Suppose you need to parse a chunk of Ruby code (and by Ruby I obviously mean Ruby 1.9). What options do you have?
I want to rip it, rip it
The obvious choice is Ripper, an external interface to the Ruby’s parser which is distributed with the MRI Ruby itself. Let’s take a look…
=> true
pry> Ripper.sexp 'def hello(greet="world"); puts "hello #{greet}"; end'
=> [:program,
[[:def,
[:@ident, "hello", [1, 4]],
[:paren,
[:params,
nil,
[[[:@ident, "greet", [1, 10]],
[:string_literal,
[:string_content, [:@tstring_content, "world", [1, 17]]]]]],
nil,
nil,
nil]],
[:bodystmt,
[[:void_stmt],
[:command,
[:@ident, "puts", [1, 26]],
[:args_add_block,
[[:string_literal,
[:string_content,
[:@tstring_content, "hello ", [1, 32]],
[:string_embexpr, [[:var_ref, [:@ident, "greet", [1, 40]]]]]]]],
false]]],
nil,
nil,
nil]]]]
I want to rip it, rip it
The obvious choice is Ripper, an external interface to the Ruby’s parser which is distributed with the MRI Ruby itself. Let’s take a look…
CODE:
pry> require 'ripper'=> true
pry> Ripper.sexp 'def hello(greet="world"); puts "hello #{greet}"; end'
=> [:program,
[[:def,
[:@ident, "hello", [1, 4]],
[:paren,
[:params,
nil,
[[[:@ident, "greet", [1, 10]],
[:string_literal,
[:string_content, [:@tstring_content, "world", [1, 17]]]]]],
nil,
nil,
nil]],
[:bodystmt,
[[:void_stmt],
[:command,
[:@ident, "puts", [1, 26]],
[:args_add_block,
[[:string_literal,
[:string_content,
[:@tstring_content, "hello ", [1, 32]],
[:string_embexpr, [[:var_ref, [:@ident, "greet", [1, 40]]]]]]]],
false]]],
nil,
nil,
nil]]]]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-745592/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用 Ruby 的 Nokogiri 庫來解析
- [Ruby]format xml with RubyORMXML
- Ruby 札記 - 淺嘗 Ruby 特性
- Ruby 札記 - 淺談和配置 Ruby
- Ruby己死?NodeJS能否取代Ruby?NodeJS
- Ruby 3.0.0釋出:Ruby3將比Ruby2快3倍
- rails on ruby,ruby on rails 之程式碼塊(二)AI
- Ruby Profiler詳解之ruby-prof(I)
- Ruby Struct EqualStruct
- Ruby loop 方法OOP
- JavaScript and Ruby in ABAPJavaScript
- Ruby初學
- Ruby Block 理解BloC
- Ruby語言
- Ruby Code & Style
- [ruby] rails renderAI
- Ruby變數變數
- Ruby 札記 - Ruby 集合家族之陣列(Array)陣列
- centos7 ruby版本過低,升級ruby版本CentOS
- Ruby程式語言與Ruby之間的比較
- RUBY實踐—Ruby Report之Ruport簡單應用
- ruby on rails 小技巧AI
- Ruby 3.1.0 釋出
- rvm管理ruby版本
- Ruby 匿名函式函式
- Ruby例外處理
- Mac上更新RubyMac
- Centos安裝rubyCentOS
- ruby連線OracleOracle
- Ruby on Rails 相關AI
- Ruby On Rails 技術AI
- RUBY-FLEX實踐—利用swfobject在RUBY工程中載入SWFFlexObject
- 理解 Ruby 裡的 blockBloC
- Ruby on Rails Ping ++ 支付AI
- Ruby中的陣列陣列
- Ruby動態類別
- Logstash ruby 外掛
- ruby jekyll gem 安裝