(noob) cast string to array?

In article 110220041931304813%user@unknown.invalid,

Hi. What a fantastic language. This is my first day learning using it
and I love it so far.

I am not very comfortable with Ruby speak yet, so I hope the
explaination of my problem makes some kind of sense.

I have a program that fetches some results from a shell script. The
result is a string, not a block as it may look like below.

result = { “foobar”, “foobar”, “foobar” }

I want to basically remove the {}'s and replace them with and cast
result as an array so that I can have some fun within Ruby.

I figured something like this:

result = result.sub(/{/,‘[’)
result = result.sub(/}/,‘]’)

Now I have a string that LOOKS like and Array but isn’t…

One thing that you might consider is that you have a line which looks
like a CSV data record with { } adound it, so you could use a library
from the Ruby Application Archive to mangle it after you’ve trimmed the
{ and }.

Alternatively, if the data is “well behaved”, you can use a regular
expression to pick it apart. Using irb (interactive ruby to experiment:

result = ‘{ “robert”, “trey”, “adrian”, “pat” }’
=> “{ "robert", "trey", "adrian", "pat" }”
result.scan(/“(.*?)”/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]

If you’re familiar with regular expressions then they are well
integrated into Ruby and can be powerful tools. If you’re not then
there have been plenty of other suggestions which may fit your needs.

Hope this helps,

Mike

···

Koncept user@unknown.invalid wrote:


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

“Koncept” user@unknown.invalid schrieb im Newsbeitrag
news:120220041122469448%user@unknown.invalid…

In article
XOCWb.33248$TPZ.8375@twister01.bloor.is.net.cable.rogers.com, Mike

One thing that you might consider is that you have a line which looks
like a CSV data record with { } adound it, so you could use a library
from the Ruby Application Archive to mangle it after you’ve trimmed
the
{ and }.

The {} is the result of issuing a command to AppleScript. This is the
way Applescript formats “Lists” or Arrays.

Alternatively, if the data is “well behaved”, you can use a regular
expression to pick it apart. Using irb (interactive ruby to
experiment:

result = ‘{ “robert”, “trey”, “adrian”, “pat” }’
=> “{ "robert", "trey", "adrian", "pat" }”
result.scan(/“(.*?)”/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]

Forgive me if I am missing something…

irb(main):001:0> result = ‘{ “robert”,“trey”,“adrian”, “pat” }’
=> “{ "robert","trey","adrian", "pat" }”

irb(main):002:0> result.scan(/“(.*?)”/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]

irb(main):004:0> result.class
=> String

irb(main):005:0> result = [“robert”,“trey”,“adrian”,“pat”]
=> [“robert”, “trey”, “adrian”, “pat”]

irb(main):006:0> result.class
=> Array

Your solution formatted my string to look like an Array, but the result
still has a class of String. :frowning:

How can I cast this String as an Array?

What do you think is the type of the result of line 002?

robert

If you’re familiar with regular expressions then they are well
integrated into Ruby and can be powerful tools. If you’re not then
there have been plenty of other suggestions which may fit your needs.

Hope this helps,

Mike


Koncept <<
“Contrary to popular belief, the most dangerous animal is not the lion
or
tiger or even the elephant. The most dangerous animal is a shark riding
on an elephant, just trampling and eating everything they see.” - Jack
Handey

···

Stok mike@stok.co.uk wrote:

I think that you are discarding the result of flatten…

(not tested)
array = result.scan(…).flatten
array.class

Cheers,
Martin

···

On Thursday 12 February 2004 16:24, Koncept wrote:

irb(main):001:0> result = ‘{ “robert”,“trey”,“adrian”, “pat” }’
=> “{ "robert","trey","adrian", "pat" }”

irb(main):002:0> result.scan(/“(.*?)”/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]

irb(main):004:0> result.class
=> String

In article 120220041122469448%user@unknown.invalid,

In article
XOCWb.33248$TPZ.8375@twister01.bloor.is.net.cable.rogers.com, Mike

One thing that you might consider is that you have a line which looks
like a CSV data record with { } adound it, so you could use a library
from the Ruby Application Archive to mangle it after you’ve trimmed the
{ and }.

The {} is the result of issuing a command to AppleScript. This is the
way Applescript formats “Lists” or Arrays.

Alternatively, if the data is “well behaved”, you can use a regular
expression to pick it apart. Using irb (interactive ruby to experiment:

result = ‘{ “robert”, “trey”, “adrian”, “pat” }’
=> “{ "robert", "trey", "adrian", "pat" }”
result.scan(/“(.*?)”/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]

Forgive me if I am missing something…

irb(main):001:0> result = ‘{ “robert”,“trey”,“adrian”, “pat” }’
=> “{ "robert","trey","adrian", "pat" }”

irb(main):002:0> result.scan(/“(.*?)”/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]

irb(main):004:0> result.class
=> String

irb(main):005:0> result = [“robert”,“trey”,“adrian”,“pat”]
=> [“robert”, “trey”, “adrian”, “pat”]

irb(main):006:0> result.class
=> Array

Your solution formatted my string to look like an Array, but the result
still has a class of String. :frowning:

irb was just showing what the value of the expression was. If you want
to apply the technique then either store the result

data = ‘{ “robert”,“trey”,“adrian”, “pat” }’
=> “{ "robert","trey","adrian", "pat" }”
result = data.scan(/“(.*?)”/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]
data.class
=> String
result.class
=> Array

or use it immediately e.g.

data.scan(/“(.*?)”/).flatten.each do |name|
?> puts “got >#{name}<”
end
got >robert<
got >trey<
got >adrian<
got >pat<
=> [“robert”, “trey”, “adrian”, “pat”]

In general ruby methods don’t change object being operated upon unless
they have a “destructive sounding name” (e.g. Array#delete) or end with
a ! (e.g. String#gsub!)

Hope this helps,

Mike

···

Koncept user@unknown.invalid wrote:

Stok mike@stok.co.uk wrote:


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

“Koncept” user@unknown.invalid schrieb im Newsbeitrag
news:120220041122469448%user@unknown.invalid…

irb(main):001:0> result = ‘{ “robert”,“trey”,“adrian”, “pat” }’
=> “{ "robert","trey","adrian", "pat" }”

irb(main):002:0> result.scan(/“(.*?)”/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]

Try

result = result.scan(/“(.*?)”/).flatten

···

On Thu, 12 Feb 2004 17:35:20 +0100, Robert Klemme wrote:

irb(main):004:0> result.class
=> String


Simon Strandgaard

Packed with Avi’s old Iowa release is a neat little extra that he called
Kansas. It’s a nifty little package that wraps database accesses in an
object interface along with some automatic inspection of table structure
so that one need not code the exact structure. One just tells it,
optionally, what the one to one and one to many relationships are between
the tables.

It’s more or less exactly the sort of thing that I was looking for, as it
lets me interacts with the database and get my result sets as objects
without requiring me to mirror database structure exactly in my code. And
to think that I ignored it for almost two years! Shameful.

Anyway, my question is – is there a more evolved/tested/commented version
of Kansas out there anywhere? If not, I am going to, with Avi’s
permission, pull it off from the Iowa code tree into it’s own package with
a few enhancements to it that I have made and make it available for
seperate download. It’s really a sweet little package.

Kirk Haines

Kirk Haines khaines@enigo.com wrote in message news:Pine.LNX.4.44.0402121144580.12056-100000@enigo.nanc.com

Anyway, my question is – is there a more evolved/tested/commented version
of Kansas out there anywhere? If not, I am going to, with Avi’s
permission, pull it off from the Iowa code tree into it’s own package with
a few enhancements to it that I have made and make it available for
seperate download. It’s really a sweet little package.

Feel free to do whatever you like with that code. It was even more of
a hack than IOWA - literally an evening or two’s work, so don’t expect
too much from it. I would be surprised if there weren’t better O/R
mapping tools for Ruby out there by now, but I haven’t kept up. Oh,
and make sure you have the version with PragDave’s modifications (if
you pulled it from CVS you should have them).

Most recently I’ve been experimenting with the idea of modelling the
relational algebra directly as (in my case) first class Smalltalk
expressions, so that you can compose and iterate over SQL queries in
an object oriented way instead of through string manipulation - kind
of like Criteria but more rigorous and (I think) more flexible. It’s
pretty nifty. I’ve blogged about it a couple of times:
http://www.cincomsmalltalk.com/userblogs/avi/blogView?searchCategory=databases

If I were going to build yet another relational mapping tool I would
start with that. Luckily all my projects right now use object
databases…

“Simon Strandgaard” neoneye@adslhome.dk schrieb im Newsbeitrag
news:pan.2004.02.12.16.43.18.895279@adslhome.dk…

“Koncept” user@unknown.invalid schrieb im Newsbeitrag
news:120220041122469448%user@unknown.invalid…

irb(main):001:0> result = ‘{ “robert”,“trey”,“adrian”, “pat” }’
=> “{ "robert","trey","adrian", "pat" }”

irb(main):002:0> result.scan(/“(.*?)”/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]

Try

result = result.scan(/“(.*?)”/).flatten

irb(main):004:0> result.class
=> String

Dunno what you did, but scan returns an array:

irb(main):004:0> result = ‘{ “robert”,“trey”,“adrian”, “pat” }’
=> “{ "robert","trey","adrian", "pat" }”
irb(main):005:0> result.scan(/“(.?)“/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]
irb(main):006:0> result.scan(/”(.
?)”/).flatten.class
=> Array
irb(main):007:0> result = result.scan(/“(.*?)”/).flatten
=> [“robert”, “trey”, “adrian”, “pat”]
irb(main):008:0> result.class
=> Array
irb(main):009:0>

robert
···

On Thu, 12 Feb 2004 17:35:20 +0100, Robert Klemme wrote:

Feel free to do whatever you like with that code. It was even more of
a hack than IOWA - literally an evening or two’s work, so don’t expect
too much from it. I would be surprised if there weren’t better O/R
mapping tools for Ruby out there by now, but I haven’t kept up. Oh,
and make sure you have the version with PragDave’s modifications (if
you pulled it from CVS you should have them).

Yes, what I have is what you had in the Iowa CVS. What I like about it is
it’s lightweight simplicity. There are definitely areas where the
library needs to be fleshed out a bit for robustness, but my initial
experiments with it show it working pretty well after some light
modifications to make it work with preexisting database connections so
that I could use my db connection pooling.

99% of my database accesses are pretty simplistic interactions. Given a
key, pull one or more records from a table, with only occasional joins
between two tables. And the only real hassle that I’ve had using SQL
and manipulating query results directly is one of convenience. I don’t
like having to explicitly maintain two seperate representations of table
data – one in the database and one in some Struct declaration so that I
have an object to put query results into. It’s ugly and a pain in the
butt sometimes. All I really want from a package is for it to do the work
of looking at a table and making a usable object representation, and for
it to give me a simple way of making queries and getting the results back
in one of those object representations. If there is a better or more
mature package for Ruby that does this in a lightweight way, I’d love to
know about it. If not, though, I’ll clean up and strengthen Kansas a bit
and be happy with it. It’s basically all that I want.

Most recently I’ve been experimenting with the idea of modelling the
relational algebra directly as (in my case) first class Smalltalk
expressions, so that you can compose and iterate over SQL queries in
an object oriented way instead of through string manipulation - kind
of like Criteria but more rigorous and (I think) more flexible. It’s
pretty nifty. I’ve blogged about it a couple of times:
http://www.cincomsmalltalk.com/userblogs/avi/blogView?searchCategory=databases

That’s really interesting. I’ll have to read it again in the morning,
though, and see if my brain can parse the Smalltalk examples better.

Thanks,

Kirk Haines

···

On Fri, 13 Feb 2004, Avi Bryant wrote: