如何将Table_Print gem作为独立的gem运行

时间:2016-04-29 20:35:19

标签: ruby-on-rails ruby rubygems

使用rails应用程序,我想使用table_print gem,但我不允许在gemfile中安装新的gem。我看到有一个独立的版本,我应该可以直接在我的机器上安装,但我不知道如何让它工作。

当我致电tp Invoice.all时,我收到以下错误。

  

NoMethodError:main:Object

的未定义方法`tp'

我尝试使用以下代码的变体创建.irbrc文件,但没有快乐。

# Outside rails
$ irb
> require 'table_print'
> tp array_of_objects, options

# Inside rails, the gem has already been required by your Gemfile so all you need to do is
$ rails c
> tp array_of_objects, options

有没有人让table_print的独立版本与rails c一起使用?

1 个答案:

答案 0 :(得分:1)

将数组直接传递给tp参数:

 ➩ ➩  irb
2.0.0-p645 :002 > require 'table_print'
 => true
2.0.0-p645 :003 > tp [{id: 1, first_name: "Tim", last_name: "Thing", email: "test@example.com", dob: "1985-01-01" },  {id: 2, first_name: "Rob", last_name: "Roberts", email: "test@example.com", dob: "1985-01-01"}, {id: 3, first_name: "Nancy", last_name: "Name", email: "test@example.com", dob: "1985-01-01"}]
ID | FIRST_NAME | LAST_NAME | EMAIL            | DOB
---|------------|-----------|------------------|-----------
1  | Tim        | Thing     | test@example.com | 1985-01-01
2  | Rob        | Roberts   | test@example.com | 1985-01-01
3  | Nancy      | Name      | test@example.com | 1985-01-01
 => 0.000762
2.0.0-p645 :004 >

或者将数组保存为变量:

 ➩ ➩  irb
2.0.0-p645 :002 > require 'table_print'
 => true
2.0.0-p645 :004 > object = [{id: 1, first_name: "Tim", last_name: "Thing", email: "test@example.com", dob: "1985-01-01" },  {id: 2, first_name: "Rob", last_name: "Roberts", email: "test@example.com", dob: "1985-01-01"}, {id: 3, first_name: "Nancy", last_name: "Name", email: "test@example.com", dob: "1985-01-01"}]
=> [{:id=>1, :first_name=>"Tim", :last_name=>"Thing", :email=>"test@example.com", :dob=>"1985-01-01"}, {:id=>2, :first_name=>"Rob", :last_name=>"Roberts", :email=>"test@example.com", :dob=>"1985-01-01"}, {:id=>3, :first_name=>"Nancy", :last_name=>"Name", :email=>"test@example.com", :dob=>"1985-01-01"}]
2.0.0-p645 :005 > tp object
 ID | FIRST_NAME | LAST_NAME | EMAIL            | DOB
 ---|------------|-----------|------------------|-----------
 1  | Tim        | Thing     | test@example.com | 1985-01-01
 2  | Rob        | Roberts   | test@example.com | 1985-01-01
 3  | Nancy      | Name      | test@example.com | 1985-01-01
 => 0.001

如果没有错误的完整痕迹,这就是我能给予的所有帮助。希望它有所帮助!

- NoMethodError让我觉得table_print不喜欢你传递它的ActiveRecord :: Relation。也许尝试将Invoice.all.map(&:attributes)传递给tp