Skip to content

Instantly share code, notes, and snippets.

@amirrajan
Last active March 16, 2021 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amirrajan/f64c811715cd19b7e9dc30eaf79eec85 to your computer and use it in GitHub Desktop.
Save amirrajan/f64c811715cd19b7e9dc30eaf79eec85 to your computer and use it in GitHub Desktop.
Ruby plot twist.
require 'ripper'
c = <<-S
def hello_world
puts "hello world"
end
S
r = "#{Ripper.sexp(c)}"
puts "#{r.gsub(",", " ").gsub("[", "(").gsub("]", ")")}"
# output
# (:program
# ((:def (:@ident "hello_world" (1 4)) (:params nil nil nil nil nil nil nil)
# (:bodystmt
# ((:command (:@ident "puts" (2 2))
# (:args_add_block
# ((:string_literal (:string_content
# (:@tstring_content "hello world" (2 8)))))
# false)))
# nil nil nil))))
# INB4:
# > Ruby is as much like Lisp as Javascript is, and I'd never call Javascript Lisp-like.
# Actually, it goes much deeper than that. The core AST for ruby uses car and cdr:
# https://github.com/mruby/mruby/blob/master/include/mruby/compile.h#L49
# Also, Ruby was originally called MatzLisp: http://web.archive.org/web/20170526172735/http://matzlisp.org/
# PS:
# And the creator of the language uses Emacs (he was inspired by it to create Ruby in fact):
# https://www.slideshare.net/yukihiro_matz/how-emacs-changed-my-life
# https://github.com/ruby/elisp/blob/master/ruby-mode.el#L7
# http://web.archive.org/web/20170526172735/http://matzlisp.org/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment