$30 off During Our Annual Pro Sale. View Details »

Vim and Ruby

pocke
December 09, 2017

Vim and Ruby

Rails Developers Meetup 2017(飛び込みLT)

pocke

December 09, 2017
Tweet

More Decks by pocke

Other Decks in Programming

Transcript

  1. Vim and Ruby
    Rails Developers Meetup 2017(LT)
    Dec. 9, 2017

    View Slide

  2. $ cat /path/to/pocke
    ● Masataka Kuwabara (Pocke)
    ● SideCI
    ● RuboCop's core developer
    ● RuboCop-JP Organizer ← New

    View Slide

  3. View Slide

  4. Vim and Ruby

    View Slide

  5. Q. attendees.use?(Vim)

    View Slide

  6. OK, everyone uses Vim!

    View Slide

  7. Vim Plugins
    ● We can get more powerful Vim with plugins.
    ○ Plugin examples: vim-rails (I don't use it), Denite.nvim,
    NeoSnippet, …
    ● Vim plug-ins are written in Vim script.
    ○ And, many "dark-powered" Vim plug-ins are written in
    Python.

    View Slide

  8. Vim script and Python...

    View Slide

  9. You can write Vim plugins
    in Ruby

    View Slide

  10. Vim Ruby
    ● We can write Vim plugins in native language.
    ● We can use gems e.g.) ActiveSupport
    ● We can use Ruby's powerful standard
    libraries
    ○ https://docs.ruby-lang.org/ja/latest/class/String.html
    ● See more information → :help ruby
    ○ http://vim-jp.org/vimdoc-ja/if_ruby.html

    View Slide

  11. How to write ruby code

    View Slide

  12. Inline Ruby script
    " In .vimrc
    ruby << RUBY
    def hello(name)
    puts "Hello, #{name}!"
    end
    hello("pocke")
    RUBY
    ruby hello('pocke') "It prints Hello, pocke!

    View Slide

  13. Inline Ruby script
    " In .vimrc
    ruby << RUBY
    def hello(name)
    puts "Hello, #{name}!"
    end
    hello("pocke")
    RUBY
    ruby hello('pocke') "It prints Hello, pocke!

    View Slide

  14. Load a ruby file from Vim script
    $ tree
    autoload/
    ├── foo.vim
    └── foo.vim.rb

    View Slide

  15. Load a ruby file from Vim script
    " In foo.vim, it loads foo.vim.rb
    let s:self_path = expand("")
    execute 'ruby require "' .
    \ s:self_path . '.rb"'

    View Slide

  16. How to communicate with
    Vim and Ruby

    View Slide

  17. Vim → Ruby
    # In a ruby script
    source =
    Vim.evaluate("getbufline('.', 1, '$')")
    .join("\n")
    puts source # editing source code is printed

    View Slide

  18. Ruby → Vim(I think it is a workaround...)
    # In Ruby script, serialize the result,
    # and assign Vim script's variable
    Vim.command "let s:result = #{res.to_json}"
    " In Vim script
    echo s:result

    View Slide

  19. Vim plugins written in Ruby
    In the real world

    View Slide

  20. github.com/pocke/iro.vim

    View Slide

  21. Iro.vim
    ● A new syntax highlighter for Ruby, YAML, and
    Python (Python support is experimental).
    ● Use parser to highlight instead of regular
    expression.
    ○ Ripper, Psych, ...
    ● It highlights perfectly.

    View Slide

  22. Iro.vim
    ● See my slide also: "The new syntax
    highlighter for vim." in VimConf 2017
    ○ https://speakerdeck.com/pocke/the-new-syntax-highl
    ighter-for-vim

    View Slide

  23. github.com/pocke/
    yaml-path.vim

    View Slide

  24. YAML-Path.vim
    ● Display a path of YAML.
    ● It uses Psych to parse a YAML code.

    View Slide

  25. View Slide

  26. Conclusion

    View Slide

  27. Conclusion
    ● We can write Vim plugins in Ruby
    ● You can find examples, iro.vim and
    yaml-path.vim.
    ● Maybe it is helpful for Rails.
    Thank you for listening!
    Follow me Twitter: @p_ck_
    GitHub: @pocke

    View Slide