pp不能处理ruby文件

时间:2012-11-28 17:01:33

标签: ruby fold perl-packager

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拼写错误code repo并cd进入repo目录。
  • (3)运行以下命令行:
    • pprfile.rb< ./app/controllers/accounts_controller.rb | awk'长度($ 0)> 79 {打印长度($ 0),$ 0}'
  • (4)我希望所有的输出行都限制在79个字符以内,但这就是我输出的内容:

    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>
    
         

1 个答案:

答案 0 :(得分: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>