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.

···

--
Posted via http://www.ruby-forum.com/.

p number.to_s

···

2013/4/1 sundar sundar <lists@ruby-forum.com>

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.

--
Posted via http://www.ruby-forum.com/\.

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

puts %Q{"#{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.

Jesus.

···

On Mon, Apr 1, 2013 at 4:02 AM, sundar sundar <lists@ruby-forum.com> wrote:

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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

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

--
Posted via http://www.ruby-forum.com/.

Joel Pearson 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.

···

--
Posted via http://www.ruby-forum.com/\.

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"]

}

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.

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)

···

On Mon, Apr 1, 2013 at 8:26 PM, sundar sundar <lists@ruby-forum.com> wrote:

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.

···

--
Posted via http://www.ruby-forum.com/\.