解析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 3.0.0釋出:Ruby3將比Ruby2快3倍
- Ruby 類
- rails on ruby,ruby on rails 之程式碼塊(二)AI
- Ruby 札記 - 縱覽優雅的 Ruby
- Ruby迭代器
- [Ruby Notes] Proc
- Ruby Struct EqualStruct
- ruby gems列表
- JavaScript and Ruby in ABAPJavaScript
- centos7 ruby版本過低,升級ruby版本CentOS
- Ruby 札記 - Ruby 集合家族之雜湊(Hash)
- Ruby 札記 - Ruby 集合家族之陣列(Array)陣列
- Logstash中的ruby
- Ruby 方法定義
- Ruby Module的用法
- rvm管理ruby版本
- Ruby 3.1.0 釋出
- Ruby例外處理
- Ruby 匿名函式函式
- ruby on rails 小技巧AI
- Ruby on Rails Ping ++ 支付AI
- Debugging Ruby in VS Code
- Ruby動態類別
- Ruby迴圈語句
- Ruby中的陣列陣列
- 使用 Ruby 擴充 Vim
- Ruby的private和protected
- 理解 Ruby 裡的 blockBloC
- ruby操作專案.xcodeprojXCode
- Jupyter Notebook新增Ruby支援
- Ruby中的true和falseFalse
- Ruby中 ?與!的慣例
- Ruby中的設計模式設計模式
- Ruby方法傳不定引數
- Ruby Profiler 詳解之 stackprof