ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 解析Ruby

解析Ruby

原创 Linux操作系统 作者:jieforest 时间:2012-10-04 11:58:55 0 删除 编辑
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…

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博客 ” ,链接:https://blog.itpub.net/301743/viewspace-745592/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-04-23

  • 博文量
    441
  • 访问量
    602353