如何在Rails中打印数组?

时间:2021-06-28 20:47:45

I am new to Rails and I am using Ruby version 1.9.3 and Rails version 3.0.0.

我是Rails的新手,我使用的是Ruby版本1.9.3和Rails版本3.0.0。

I want to print an array in Rails. How do I do that?

我想在Rails中打印一个数组。我怎么做?

For example, we have to use print_r to print an array in PHP:

例如,我们必须使用print_r在PHP中打印数组:

<?php

$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);

?>

Output:

<pre>
Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)
</pre>

How do I print an array in Rails?

如何在Rails中打印数组?

4 个解决方案

#1


10  

You can use inspect like:

您可以使用如下检查:

@a = ['a', 'b']
p @a #['a', 'b']

Or:

p @a.inspect #"[\"a\", \"b\"]"

#2


2  

You need to use awesome_print gem.

你需要使用awesome_print gem。

require 'awesome_print'

hash = {:a=>1,:b=>2,:c => [1,2,3]}

ap hash

output:

{
    :a => 1,
    :b => 2,
    :c => [
        [0] 1,
        [1] 2,
        [2] 3
    ]
}

#3


0  

It depends on what you want to use the array for.

这取决于你想要使用数组的内容。

To blindly output an array in a view, which has to be in a view, you should use debug and inspect like this:

要在视图中盲目地输出数组(必须在视图中),您应该使用debug和inspect,如下所示:

<%= @array.inspect() %>

<%= debug @array %>

However, if you want to iterate through an array, or do things like explode(), you'll be better suited using the Ruby array functions.

但是,如果你想遍历一个数组,或者像explode()这样做,你将更适合使用Ruby数组函数。

#4


0  

You've got a couple of options here. I'm assuming you're doing this in an ERB template.

你有几个选择。我假设你在ERB模板中这样做。

This will convert the array to YAML and print it out surrounded in <pre> tags

这会将数组转换为YAML并将其打印出来并包含在

标记中

<%= debug [1,2,3,4] %> 

And this will print it out formatted in a readable Ruby syntax:

这将打印出来,格式为可读的Ruby语法:

<pre><%= [1,2,3,4].inspect %></pre>

Check out "Debugging Rails Applications" for more info.

有关详细信息,请查看“调试Rails应用程序”。

#1


10  

You can use inspect like:

您可以使用如下检查:

@a = ['a', 'b']
p @a #['a', 'b']

Or:

p @a.inspect #"[\"a\", \"b\"]"

#2


2  

You need to use awesome_print gem.

你需要使用awesome_print gem。

require 'awesome_print'

hash = {:a=>1,:b=>2,:c => [1,2,3]}

ap hash

output:

{
    :a => 1,
    :b => 2,
    :c => [
        [0] 1,
        [1] 2,
        [2] 3
    ]
}

#3


0  

It depends on what you want to use the array for.

这取决于你想要使用数组的内容。

To blindly output an array in a view, which has to be in a view, you should use debug and inspect like this:

要在视图中盲目地输出数组(必须在视图中),您应该使用debug和inspect,如下所示:

<%= @array.inspect() %>

<%= debug @array %>

However, if you want to iterate through an array, or do things like explode(), you'll be better suited using the Ruby array functions.

但是,如果你想遍历一个数组,或者像explode()这样做,你将更适合使用Ruby数组函数。

#4


0  

You've got a couple of options here. I'm assuming you're doing this in an ERB template.

你有几个选择。我假设你在ERB模板中这样做。

This will convert the array to YAML and print it out surrounded in <pre> tags

这会将数组转换为YAML并将其打印出来并包含在

标记中

<%= debug [1,2,3,4] %> 

And this will print it out formatted in a readable Ruby syntax:

这将打印出来,格式为可读的Ruby语法:

<pre><%= [1,2,3,4].inspect %></pre>

Check out "Debugging Rails Applications" for more info.

有关详细信息,请查看“调试Rails应用程序”。