How to quote variable of an array

Consider the following example;
#!/usr/bin/env ruby
a = [1,2,3,4,5]
a.each { |number|
puts number
}

this produces following result
1
2
3
4
5

Now, what i want is to list the output with quote
“1”
“2”
“3”
“4”
“5”

How do i do that? The above is a model example which is a part of
program.

p number.to_s

2013/4/1 sundar sundar [email protected]

On Mon, Apr 1, 2013 at 4:02 AM, sundar sundar [email protected]
wrote:

Consider the following example;
#!/usr/bin/env ruby
a = [1,2,3,4,5]
a.each { |number|
puts number

puts %Q{“#{number}”}

“1”
“2”
“3”
“4”
“5”

How do i do that? The above is a model example which is a part of
program.

Jesus.

Thank you. Both the answers worked. I have chosen Jesu’s answer though.

If you “p” (inspect) a string, then you’ll get quotes because that’s how
they’re represented.

If you use Jesus’ method, you can add whatever you want around the
string.

Bear in mind though, that the representation of Fixnum 1 is 1, and
String 1 is “1”. If you manually add quotes you might end up
misrepresenting the type of object you’re using.

arr = [‘a’, ‘b’, ‘c’]

arr.each_with_index do |char, i|
puts i
puts char
puts ‘*****’
end

–output:–
0
a


1
b


2
c


arr = [[‘a’, ‘b’, ‘c’]]

arr.each_with_index do |char, i|
puts i
puts char
puts ‘*****’
end

–output:–
0
a
b
c


How many times did the last example loop? Do the number of lines in the
output tell you how many times the loop executed? When you are
debugging, p is your friend, puts is not:

arr =
puts arr

–output:–
a
b
c

puts makes it look like arr has three elements. But not so fast:

p arr

–output:–
[[“a”, “b”, “c”]]

Now, it’s clear that arr has only one element, which is an array.
Hopefully, the result will show you that whoever programmed
the method puts() really screwed up, so you will have to learn
never to trust the output of puts.

Joel P. wrote in post #1103948:

If you “p” (inspect) a string, then you’ll get quotes because that’s how
they’re represented.

If you use Jesus’ method, you can add whatever you want around the
string.

Bear in mind though, that the representation of Fixnum 1 is 1, and
String 1 is “1”. If you manually add quotes you might end up
misrepresenting the type of object you’re using.

Thank you. The problem is not solved. The above example does not work
when i implement it to the real code . Here is the actual code. I am
trying to find usb details using dbus (ruby-dbus gem).

#!/usr/bin/env ruby

require ‘rubygems’
require ‘dbus’

bus = DBus::SystemBus.instance
udisk_service = bus.service(“org.freedesktop.UDisks”)
udisk_udisk = udisk_service.object("/org/freedesktop/UDisks")
udisk_udisk.introspect
udisk_udisk.default_iface = “org.freedesktop.UDisks”
disk_list = [udisk_udisk.EnumerateDevices]

disk_list.each { |disk|
#puts %Q{"#{disk}"}
puts disk
udisk_udisk = udisk_service.object("#{disk}")
udisk_udisk.introspect
udisk_udisk_i = udisk_udisk[“org.freedesktop.UDisks.Device”]
puts udisk_udisk_i[“IdUuid”]
}

disk_list is array of available disks attached to system. When i use
“put disk” in the loop i list all the disk with out problem. Like this
/org/freedesktop/UDisks/devices/sr0
/org/freedesktop/UDisks/devices/sdc
/org/freedesktop/UDisks/devices/sdb
/org/freedesktop/UDisks/devices/sda
/org/freedesktop/UDisks/devices/sda1
/org/freedesktop/UDisks/devices/sda2
/org/freedesktop/UDisks/devices/sda3
/org/freedesktop/UDisks/devices/sdc1
/org/freedesktop/UDisks/devices/sda5
/org/freedesktop/UDisks/devices/sda6
/org/freedesktop/UDisks/devices/sdb1
/org/freedesktop/UDisks/devices/sda7
/org/freedesktop/UDisks/devices/sda8

But when i quote with either “puts %Q{”#{disk}"}" or “p disk.to_s” then
the output becomes
“/org/freedesktop/UDisks/devices/sr0/org/freedesktop/UDisks/devices/sdc/org/freedesktop/UDisks/devices/sdb/org/freedesktop/UDisks/devices/sda/org/freedesktop/UDisks/devices/sda1/org/freedesktop/UDisks/devices/sda2/org/freedesktop/UDisks/devices/sda3/org/freedesktop/UDisks/devices/sdc1/org/freedesktop/UDisks/devices/sda5/org/freedesktop/UDisks/devices/sda6/org/freedesktop/UDisks/devices/sdb1/org/freedesktop/UDisks/devices/sda7/org/freedesktop/UDisks/devices/sda8”

What i want is that to list available disk with quote like this

“/org/freedesktop/UDisks/devices/sr0”
“/org/freedesktop/UDisks/devices/sdc”
“/org/freedesktop/UDisks/devices/sdb”
.
.
.
“/org/freedesktop/UDisks/devices/sda8”

so that the line “udisk_udisk = udisk_service.object(”#{disk}")" takes
disk one by one.

When i provide disk_list data manually with

["/org/freedesktop/UDisks/devices/sr0",
“/org/freedesktop/UDisks/devices/sdc”,"/org/freedesktop/UDisks/devices/sdb",
…, “/org/freedesktop/UDisks/devices/sda8”]

and execute looping then it works.

Any help is appriciated.

–output:–
0
a
b
c


This is what exactly happend.
Thanks for the help. Now the problem is solved.

7stud – wrote in post #1104028:

arr =
puts arr

–output:–
a
b
c

Whoops. Forgot the loop:

arr =

arr.each do |x|
puts x
end

–output:–
a
b
c

puts makes it look like arr has three elements. But not so fast:

arr.each do |x|
p x
end

–output:–
[“a”, “b”, “c”]

Now, it’s clear that arr has only one element, which is an array.

On Mon, Apr 1, 2013 at 8:26 PM, sundar sundar [email protected]
wrote:

disk_list = [udisk_udisk.EnumerateDevices]

This looks interesting. What actually does #EnumerateDevice return? In
that construction, you are wrapping that in an array. Is it already an
array of strings? I suggest at this point doing p disk_list to see
what it actually contains. My suspicion is that you’ve got an array of
array of strings there, but I don’t know the method, obviously.

disk_list.each { |disk|
#puts %Q{“#{disk}”}
puts disk

If this is the source of the lines below…

udisk_udisk = udisk_service.object(“#{disk}”)
udisk_udisk.introspect
udisk_udisk_i = udisk_udisk[“org.freedesktop.UDisks.Device”]

… what does the following line show? (and where is it in the output?)

puts udisk_udisk_i[“IdUuid”]

/org/freedesktop/UDisks/devices/sda3

so that the line “udisk_udisk = udisk_service.object(”#{disk}“)” takes
disk one by one.

When i provide disk_list data manually with

[“/org/freedesktop/UDisks/devices/sr0”,
“/org/freedesktop/UDisks/devices/sdc”,“/org/freedesktop/UDisks/devices/sdb”,
…, “/org/freedesktop/UDisks/devices/sda8”]

and execute looping then it works.

Here is where I think you’ve got something different in disk_list than
you expect. Checking the output of the #EnumerateDevice method should
show this.

If instead of the above manual list, try this:

[[“/org/freedesktop/UDisks/devices/sr0”,
“/org/freedesktop/UDisks/devices/sdc”,“/org/freedesktop/UDisks/devices/sdb”,
…, “/org/freedesktop/UDisks/devices/sda8”]]

(extra array wrapped around it)

This looks interesting. What actually does #EnumerateDevice return? In
that construction, you are wrapping that in an array. Is it already an
array of strings?
The mistake is from my part. As you said #EnumerateDevice produces array
of string. So the final output became array inside array inside array
and so on.

If instead of the above manual list, try this:

[["/org/freedesktop/UDisks/devices/sr0",
“/org/freedesktop/UDisks/devices/sdc”,"/org/freedesktop/UDisks/devices/sdb",
…, “/org/freedesktop/UDisks/devices/sda8”]]

Got it. It produces the same error listed in the first post. Corrected
it. Problem is solved.