官术网_书友最值得收藏!

Using p

Another way to print to the console is as follows:

p "B string" 

The difference between these two methods is that the former method will not return any value back to you, whereas the latter one will return a value. The following image shows this difference:

You will see that the puts method returns a value of nil:

The second option returns the value.

Though this difference may not be much now, you will discover how important this is as we progress through the course.

Another difference between the two methods is the way they process the array data structure. The p method prints the array in its code form and returns these values back to the user:

On the other hand, the puts method iterates through the collection to display individual values, and as mentioned earlier, this returns a nil value:

The first set of values were printed with the method.

Knowing that both of these methods work with all data types including arrays and collections is important. Understanding these similarities and differences is important, to ensure that you use the right method at the right time.

主站蜘蛛池模板: 嵊州市| 通榆县| 威海市| 许昌市| 湛江市| 日土县| 包头市| 高密市| 肥城市| 樟树市| 安远县| 青川县| 永顺县| 桓台县| 柏乡县| 苗栗市| 同心县| 永福县| 闸北区| 青龙| 竹溪县| 光泽县| 乌恰县| 莱阳市| 珲春市| 大同市| 微山县| 舞钢市| 玉龙| 平原县| 五台县| 万安县| 临夏县| 中牟县| 南华县| 临汾市| 五莲县| 山阴县| 上犹县| 田阳县| 甘谷县|