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:
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.
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:
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.
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
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:
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!)
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.
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…
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.