-4

如pp文档所述,如何让 pp 将行长换行(限制)为 79 个字符?

以下示例演示了 pp 不将输出限制为默认长度 79,如pp文档中所述。

  • (1) 将以下代码放入文件(pprfile.rb)。
#!/usr/bin/env ruby
require 'pp'
STDIN.read.split("\n").each do |l|
  PP.pp(l)
end

  • (2) 将 Github 错字代码repo 和 cd 克隆到 repo 目录中。
  • (3) 运行以下命令行:
    • pprfile.rb < ./app/controllers/accounts_controller.rb | awk '长度($0) > 79 { 打印长度($0), $0 }'
  • (4) 我希望所有的输出行都限制在小于或等于 79 个字符,但这是 II 得到的输出:

    89 "      redirect_back_or_default :controller => \"admin/dashboard\", :action => \"index\""
    94 "      self.current_user = User.authenticate(params[:user][:login], params[:user][:password])"
    82 "          self.current_user.remember_me unless self.current_user.remember_token?"
    82 "        add_to_cookies(:typo_user_profile, self.current_user.profile_label, '/')"
    91 "        redirect_back_or_default :controller => \"admin/dashboard\", :action => \"index\""
    80 "    @page_title = \"#{this_blog.blog_name} - #{_('Recover your password')}\""
    124 "      @user = User.find(:first, :conditions => [\"login = ? or email = ?\", params[:user][:login], params[:user][:login]])"
    108 "        flash[:notice] = _(\"An email has been successfully sent to your address with your new password\")"
    88 "    redirect_to(:controller => \"accounts\", :action => \"signup\") if User.count == 0"
    92 "    redirect_to :controller => \"setup\", :action => \"index\" if  ! this_blog.configured?"</li>
    

4

1 回答 1

1

我猜是因为“pp”只原子数据元素之间中断,而不是它们内部。原子元素将是非复合类型(例如字符串、符号、数字等)。

也就是说,如果您有一个数组,其序列化元素将超过 79 列,则“pp”将序列化该数组,并在元素之间使用换行符。但是,如果其中一个元素是 100 个字符的字符串,那么该元素将按原样序列化,大概是因为“pp”不想决定在哪里破坏该“原子”元素。

考虑:

pp(Array.new(100, 'foo')) # => <breaks between each element>
pp('x' * 100) # => <displays on one line since it doesn't know where to break>
于 2012-11-28T17:34:15.553 回答