如何 运行 Table_Print gem 独立 gem

How to run Table_Print gem as stand alone gem

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

当我调用 tp Invoice.all 时,我在 return 中收到以下错误。

NoMethodError: undefined method `tp' for main:Object

我尝试使用以下代码的变体创建 .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 一起工作?

将数组直接传递给 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