rails console_您可以通过配置〜/ .irbrc With使用Rails Console做更多的事情

rails console

与等待第一个可用的程序员变得有生产力相比,等待生产率高的程序员变得更好。 — 史蒂夫·麦康奈尔

什么是〜/ .irbrc文件? 🔖

  • 使用多个Rails应用程序后,您可能会拥有一些使用它们的方法和首选方法。
  • 也就是说,您在单个项目中使用的是几种通用方法或配置IRB会话(rails控制台),并且希望在您正在处理的其他项目中使用相同的方法和配置,则可以在〜/中添加这些方法和配置。 .irbrc文件。
  • 您可以在根目录中创建〜/ .irbrc文件(如果不存在)。

您可以在 〜/ .irbrc 文件中包含的内容🗄

  • 所有Rails Apps中都需要使用的配置
  • 所有Rails Apps中都需要使用的方法
  • 要求/加载特定文件或宝石
  • 方便的技巧,可帮助您玩耍物品

我的.irbrc文件 🧮

require 'irb/completion'
require 'rubygems'

ActiveRecord::Base.logger.level = 1 if defined ?(ActiveRecord)
IRB.conf[ :SAVE_HISTORY ] = 1000

# Overriding Object class
class Object
  # Easily print methods local to an object's class
  def lm
    (methods - Object.instance_methods).sort
  end

  # look up source location of a method
  def sl (method_name)
    self .method(method_name).source_location rescue " #{method_name} not found"
  end

  # open particular method in vs code
  def ocode (method_name)
    file, line = self .sl(method_name)
    if file && line
      `code -g ' #{file} : #{line} '`
    else
      "' #{method_name} ' not found :(Try #{ self .name} .lm to see available methods"
    end
  end

  # display method source in rails console
  def ds (method_name)
    self .method(method_name).source.display
  end

  # open json object in VS Code Editor
  def oo
    tempfile = File.join(Rails.root.join( 'tmp' ), SecureRandom.hex)
    File.open(tempfile, 'w' ) { |f| f << self .as_json}
    system( " #{ 'code' || 'nano' } #{tempfile} " )
    sleep( 1 )
    File.delete( tempfile )
  end
end

# history command
def hist (count = 0 )
  # Get history into an array
  history_array = Readline::HISTORY.to_a

  # if count is > 0 we'll use it.
  # otherwise set it to 0
  count = count > 0 ? count : 0

  if count > 0
    from = history_array.length - count
    history_array = history_array[from..- 1 ]
  end

  print history_array.join( "\n" )
end

# copy a string to the clipboard
def cp (string)
  `echo " #{string} " | pbcopy`
  puts "copied in clipboard"
end

# reloads the irb console can be useful for debugging .irbrc
def reload_irb
  load File.expand_path( "~/.irbrc" )
  # will reload rails env if you are running ./script/console
  reload! if @script_console_running
  puts "Console Reloaded!"
end

# opens irbrc in vscode
def edit_irb
  `code ~/.irbrc` if system( "code" )
end

def bm
  # From http://blog.evanweaver.com/articles/2006/12/13/benchmark/
  # Call benchmark { } with any block and you get the wallclock runtime
  # as well as a percent change + or - from the last run
  cur = Time.now
  result = yield
  print " #{cur = Time.now - cur} seconds"
  puts " ( #{(cur / $last_benchmark * 100 ).to_i - 100 } % change)" rescue puts ""
  $last_benchmark = cur
  result
end

# exit using `q`
alias q exit

# all available methods explaination
def ll
  puts '============================================================================================================='
  puts 'Welcome to rails console. Here are few list of pre-defined methods you can use.'
  puts '============================================================================================================='
  puts 'obj.sl(:method) ------> source location e.g lead.sl(:name)'
  puts 'obj.ocode(:method) ---> open method in vs code e.g lead.ocode(:name)'
  puts 'obj.dispsoc(:method) -> display method source in rails console e.g lead.dispsoc(:name)'
  puts 'obj.oo ---------------> open object json in vs code e.g lead.oo'
  puts 'hist(n) --------------> command history e.g hist(10)'
  puts 'cp(str) --------------> copy string in clipboard e.g cp(lead.name)'
  puts 'bm(block) ------------> benchmarking for block passed as an argument e.g bm { Lead.all.pluck(:stage);0 }'
  puts '============================================================================================================='
end

如何搜索其他开发人员创建的.irbrc文件file

只需单击以下URL,它就会列出其他GitHub开发人员创建的所有〜/ .irbrc文件,您可以创建文件并添加所需的所有内容(如果发现有用的话)-

参考📌

觉得这篇文章有用吗? 请分享以帮助他人找到它! 随时在下面发表评论。

翻译自: https://hackernoon.com/you-can-do-more-with-rails-console-by-configuring-irbrc-2f113usf

rails console

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值