撬-> Ruby的REPL,值得

替换


膝上开发世界中的REPL代表Read→Evaluate→Print Loop 。 读取,执行,打印等等。


像其他许多语言一样,Ruby也有自己的REPL实现irb 。 尽管它可以应付原始任务,但仍不能完全满足有洞察力的开发人员的需求。 但是还有一个更发达的替代方案: pry


撬徽标


以下是pry官方网站的摘录:


Pry是标准Ruby IRB包装器的强大替代品。 通过语法高亮显示灵活的插件体系结构运行时调用以及查看源代码文档

这不仅是广告。 没错 我将展示如何使用所有功能来安装和配置pry ,以满足最复杂的开发人员。 假定Ruby的当前工作版本已安装并在目标计算机上运行。


准备工作


转到控制台并运行以下命令:


 $ gem install pry pry-theme awesome_print coderay 

一切,我们已经完全准备就绪。 让我们进行pry ...并确保与irb相比没有区别。 突然不是很好。


那么,让我们教pry变得更聪明。


配置文件


这是.pryrc 。 主目录中已经有一个配置文件,该文件告诉pry如何在体面的社会中看起来和行为举止。 如果没有,则可以使用cd && touch .pryrc创建它。


精致的.pryrc填充说明


我将逐步演示pry的各种功能。 对于不耐烦的人:请gist .pryrc



默认编辑器


 Pry.editor = 'vi' # 'code', 'subl' 

配置将用于编辑当前上下文的editedit命令)。


命令提示


 Pry.config.prompt = [ ->(_obj, _nest_level, _) { "✎ " }, ->(*) { " " } ] 

.pryrc只是一个.pryrc文件,因此此过程可以执行您想要的任何代码,甚至可以在等待下一个提示出现时挖掘比特币。


可以以不同方式绘制第一层和嵌套层(内部块)。 以前,我使用了一个黑色阴影的三角形,该三角形为第一级切出了一个哑解析器 ,为嵌套的切出了 ▷,但逐渐得出的结论是,代码片段中没有任何多余的字符使得将片段复制到任何地方都变得更加容易。 因此。


色彩


为了支持主题,使用了pry-theme宝石。 我使用语法着色(无聊时,我运行PRY_BW=true pry )。 对于运行Rails的文件夹(包含Rails项目),颜色可能更漂亮,但是我讨厌Rails ,因此我不确定那里发生了什么。


 unless ENV['PRY_BW'] Pry.color = true Pry.config.theme = "railscasts" Pry.config.prompt = PryRails::RAILS_PROMPT if defined?(PryRails::RAILS_PROMPT) Pry.config.prompt ||= Pry.prompt end 

故事


这只是一个发现。 在调试模式下,或者如果在此之前立即执行了命令,只需按⏎,它就会重复执行。 这对于在调试器中切换代码非常有用(例如,如果您完全使用调试器,并且不以固定的外观修复所有错误,例如,不要像我一样启动调试器)。


 Pry.config.history.should_save = true Pry::Commands.command /^$/, "repeat last command" do _pry_.run_command Pry.history.to_a.last end 

队伍


调试器 这辆车不是我的自行车,我只是从我的队友那里借了这5条线,他一生都在调试,但是他们说这行得通。


 Pry.commands.alias_command 'c', 'continue' rescue nil Pry.commands.alias_command 's', 'step' rescue nil Pry.commands.alias_command 'n', 'next' rescue nil Pry.commands.alias_command 'f', 'finish' rescue nil Pry.commands.alias_command 'l', 'whereami' rescue nil 

模块/类说明配置


 Pry.config.ls.separator = "\n" # new lines between methods Pry.config.ls.heading_color = :magenta Pry.config.ls.public_method_color = :green Pry.config.ls.protected_method_color = :yellow Pry.config.ls.private_method_color = :bright_black 

似乎没有什么可评论的。


呼叫系统实用程序


Pry支持调用系统实用程序(只需在命令名称前添加一个点,而不能使用空格, .ls.ps axu )。


外挂程式


以下是插件配置的最简单示例。 没有人喜欢肿的配置,我们会对其进行抛光,并使用不同的机器将其携带数十年。 以下代码段仅是一个示例(适用于awesome插件)。 代码中的注释说明了一切。


 # `awesome_print` gem is a great syntax colorized printing # look at `~/.aprc` for more settings for awesome_print begin require 'awesome_print' # The following line enables awesome_print for all pry output, # and it also enables paging Pry.config.print = proc {|output, value| Pry::Helpers::BaseHelpers.stagger_output("=> #{value.ai}", output)} # If you want awesome_print without automatic pagination, use the line below module AwesomePrint Formatter.prepend(Module.new do def awesome_self(object, type) return super(object, type) unless type == :string return super(object, type) unless @options[:string_limit] return super(object, type) unless object.inspect.to_s.length > @options[:string_limit] colorize(object.inspect.to_s[0..@options[:string_limit]] + "...", type) end end) end AwesomePrint.defaults = { :string_limit => 80, :indent => 2, :multiline => true } AwesomePrint.pry! rescue LoadError => err puts "gem install awesome_print # <-- highly recommended" end 

定制团队


我是否提到过pry非常酷的REPL? 我们甚至可以定义自己的一组要在pry内部使用的命令。 下面的示例显示了如何创建sql命令以从控制台执行纯SQL(前提是我们拥有有效的AR连接),并且击键次数最少。


 default_command_set = Pry::CommandSet.new do command "sql", "Send sql over AR." do |query| if ENV['RAILS_ENV'] || defined?(Rails) pp ActiveRecord::Base.connection.select_all(query) else pp "No rails env defined" end end end Pry.config.commands.import default_command_set 

猴子补丁和全局


是的,我们可以使用自己的monkeypatches定制和执行任何Ruby代码,这些代码仅在pry会话中可用。 例如,我发现此代码对于测试使用数组和哈希的代码非常方便。


 class Array def self.sample(count = 10, &block) Array.new(count, &(block || :succ)) end end Hash.singleton_class.prepend(Module.new def sample(count = 10) (?a...count.times.reduce(?a) { |o| o.succ }). map(&:to_sym).zip(0...count).to_h end end) 

注意:如果您认为上述代码片段因此Integer#succ过于复杂,则可能从未处理过超过26个键的长哈希值:)


色彩调整


下面的所有内容均用于使用coderay宝石调整颜色。 结果值得。 符号是红色,数字是蓝色,以及所有爵士乐。


 CodeRay.scan("example", :ruby).term # just to load necessary files $LOAD_PATH << File.dirname(File.realpath(__FILE__)) require "escaped_colors" module CodeRay module Encoders class Terminal < Encoder TERM_TOKEN_COLORS.each_pair do |key, value| TOKEN_COLORS[key] = value end end end end 

总结


我希望这篇文章给人一个什么样的印象,以及为什么它比irb更好的印象。 我没有特别关注调试过程,主要是因为我不是在调试,而是从头开始编写正确的代码。 但我希望我能对那些第一次看到这种字母组合的人感兴趣-至少尝试一下。 相信我,这是值得的。


好副本!

Source: https://habr.com/ru/post/zh-CN481728/


All Articles