Is there a better string.each?

tests/recno.rb:203:in `open'(TestRecno): Permission denied (BDB::Fatal)

The problem is here, try this modification

pigeon% diff -u recno.rb~ recno.rb
--- recno.rb~ Sun Mar 3 15:23:12 2002
+++ recno.rb Mon Jul 8 18:30:03 2002
@@ -194,6 +194,7 @@
    end
=end
    def test_12_env
+ $bdb.close
       Dir.foreach('tmp') do |x|
         if FileTest.file?("tmp/#{x}")
            File.unlink("tmp/#{x}")
pigeon%

Guy Decoux

sth = dbh.prepare("select au_lname, au_fname from authors where state = #{state}")

[...]

I get:

/usr/local/lib/ruby/site_ruby/1.6/DBD/ODBC/ODBC.rb:129:in `prepare': S0022 (207)
[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'CA'.
(DBI::DatabaseError)
        from /usr/local/lib/ruby/site_ruby/1.6/dbi/dbi.rb:536:in `prepare'
        from select_1.rb:8

Is there a problem in using where clauses in DBI with ODBC? Or am I
doing something wrong?

You forgot the single-quotes around the string literal in your SQL:

sth = dbh.prepare("select au_lname, au_fname from authors where state = '#{state}'")

WIA,

-- Dossy

···

On 2002.07.09, Mark Probert <probertm@nortelnetworks.com> wrote:

--
Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
  "He realized the fastest way to change is to laugh at your own
    folly -- then you can let go and quickly move on." (p. 70)

Mark,

I don’t know MS-SQL, but I would estimate that you need to quote your values
unless you want the dbms to look for a column of that name:

dbh.prepare(“select … from … where state=’#{state}’”)
^ ^
Cheers
Hannes

This is a Ruby-DBI question. I am using 0.0.15 under ruby 1.6.6 (cygwin).
I am using ODBC to connect to a SQL 7 database. The connection and
test programs work fine.

However, when I do:

require ‘dbi’

dbh = DBI.connect(‘DBI:ODBC:pubs’, ‘bob’, ‘bob’)
state = “CA”
sth = dbh.prepare(“select au_lname, au_fname from authors where state =
#{state}”)
sth.execute

I get:

/usr/local/lib/ruby/site_ruby/1.6/DBD/ODBC/ODBC.rb:129:in prepare': S0022 (207) [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'CA'. (DBI::DatabaseError) from /usr/local/lib/ruby/site_ruby/1.6/dbi/dbi.rb:536:in prepare’
from select_1.rb:8

Is there a problem in using where clauses in DBI with ODBC? Or am I
doing something wrong?

You need to put quotes around the #{state}substitution to compare the value
of the state column to a string literal “CA”. Without quotes, the value of
the state column is compared to the value of the CA column, which I guess
doesn’t exist and results in the error message you are seeing. The
statement should be built like this:

sth = dbh.prepare(“select au_lname, au_fname from authors where state =
"#{state}"”)

You might be able to use single quotes in SQL (I can’t remember; it’s a
while since I’ve done any SQL coding), in which case the statement can be
built like this, which is more readable:

sth = dbh.prepare(“select au_lname, au_fname from authors where state =
‘#{state}’”)

Cheers,
Nat.

···

From: “Mark Probert” probertm@nortelnetworks.com


Dr. Nathaniel Pryce
B13media Ltd.
Studio 3a, Aberdeen Business Centre, 22/24 Highbury Grove, London, N5 2EA
http://www.b13media.com

Better write:

sth = dbh.prepare(“select au_lname, au_fname from authors where state = ?”)
sth.execute(state)

or even shorter:

dbh.execute(“select au_lname, au_fname from authors where state = ?”, state)

Regards,

Michael

···

On Tue, Jul 09, 2002 at 01:10:36AM +0900, Mark Probert wrote:

Hi.

This is a Ruby-DBI question. I am using 0.0.15 under ruby 1.6.6 (cygwin).
I am using ODBC to connect to a SQL 7 database. The connection and
test programs work fine.

However, when I do:

require ‘dbi’

dbh = DBI.connect(‘DBI:ODBC:pubs’, ‘bob’, ‘bob’)
state = “CA”
sth = dbh.prepare(“select au_lname, au_fname from authors where state = #{state}”)
sth.execute

Tom Sawyer wrote:

but that left us with no equivalent to the fictional #each_chr,

so what about a simple RCR, asking for

String#chars
, so that we can do
‘foo’.chars #> [“f”,“o”,“o”]
and
‘foo’.chars.each do |char|
perhaps plus String#each_char?

Tobi

···


http://www.pinkjuice.com/

Hello –

···

On Tue, 9 Jul 2002, Tom Sawyer wrote:

since $/ can’t be a regexp, a shorthand was desired to append multiple
successive \n’s together in the #each method and unfortunetly ‘’ got the
job. but that left us with no equivalent to the fictional #each_chr,

You can do: string.split(“”)

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

I agree. However, even though the code may be different, that’s
basically what String#each is doing already, except that it’s by a
string.

foo.each(bar) …

could be implemented as

foo.split(/#{bar}/).each …

very easily, with the one difference that if bar.empty?, then it’s
treated as:

foo.split(/\n+/m)).each …

I think what I’m getting at is that String#each already does the
equivalent of String#split#each – but does so in a way that is less
useful than String#split#each (IMO).

Why not make String#each act like String#split#each?

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.07.08 at 17.33.30

···

On Tue, 9 Jul 2002 05:55:28 +0900, David Alan Black wrote:

On Mon, 8 Jul 2002, Austin Ziegler wrote:

If $/ (the argument to String#each) could be made to accept
regex, then the record separator could be String#each(//) if
necessary, allowing character-by-character parsing. This is not
currently possible.
I’ll put in (yet another :slight_smile: plug for String#split:

str.split(//).each …

I think the idea of having String#each take a regex is,
essentially, a kind of compression of this – in other words,
instead of #split’ing on a regex and then iterating through the
resulting array, moving the regex intelligence into the argument
to #each. I’m of two minds about the merits of this, but in any
case I don’t think there would be anything possible in the
language that isn’t possible now.

david,

str.split(//).each …

this involves excess overhead just to get each character. better to use:

str.each_byte { |c| c.chr }

but either way both of these take two steps when only one should be
needed.

str.each(‘’) …

or

$/ = ‘’
str.each …

is much more elegent and can be implemented in such a way as to be more
efficient.

~transami

···

On Mon, 2002-07-08 at 14:55, David Alan Black wrote:

Hello –

On Mon, 8 Jul 2002, Austin Ziegler wrote:

If $/ (the argument to String#each) could be made to accept regex,
then the record separator could be String#each(//) if necessary,
allowing character-by-character parsing. This is not currently
possible.

I’ll put in (yet another :slight_smile: plug for String#split:

str.split(//).each …

I think the idea of having String#each take a regex is, essentially, a
kind of compression of this – in other words, instead of #split’ing
on a regex and then iterating through the resulting array, moving the
regex intelligence into the argument to #each. I’m of two minds about
the merits of this, but in any case I don’t think there would be
anything possible in the language that isn’t possible now.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav


~transami

“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin

Hi,

“Olonichev Sergei” olonichev@scnsoft.com writes:

:I have compiled Berkeley DB 3.3.11 and bdb-0.3.1 under cygwin.
:But “make test” does not work well. Have anybody tested bdb under cygwin?

http://www.ruby-lang.org/~eban/ruby/binaries/cygwin/ext/bdb-0.3.1-i386-cygwin.tar.gz

Try this:

— tests/btree.rb.orig Sat Mar 16 22:36:05 2002
+++ tests/btree.rb Sun Mar 17 21:12:39 2002
@@ -224,2 +224,3 @@
assert_equal(“KLMTU”, $bdb[“yellow”], “”)

  •  $bdb.close
    
    end
    @@ -247,3 +248,4 @@
    $bdb = nil
  •  assert_kind_of(BDB::Btree, BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  assert_kind_of(BDB::Btree, unknown = BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  unknown.close
    
    end
    @@ -313,2 +315,3 @@
    assert_equal(nil, $bdb.close, “”)
  •  $env.close
    
    end
    @@ -378,3 +381,3 @@
    $hash.each_key do |k|
  •   assert($bdb.key?(k), "<key>")
    
  •   assert(!!$bdb.key?(k), "<key>")
    
    end
    — tests/hash.rb.orig Sat Mar 16 22:39:50 2002
    +++ tests/hash.rb Sun Mar 17 21:12:39 2002
    @@ -215,3 +215,4 @@
    $bdb = nil
  •  assert_kind_of(BDB::Hash, BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  assert_kind_of(BDB::Hash, unknown = BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  unknown.close
    
    end
    @@ -281,2 +282,4 @@
    def test_17_hash_delete
  •  assert_equal(nil, $bdb.close, "<close>")
    
  •  $env.close
     clean
    

@@ -343,3 +346,3 @@
$hash.each_key do |k|

  •   assert($bdb.key?(k), "<key>")
    
  •   assert(!!$bdb.key?(k), "<key>")
    
    end
    — tests/queue.rb.orig Sun Mar 3 23:21:12 2002
    +++ tests/queue.rb Sun Mar 17 21:12:39 2002
    @@ -168,3 +168,4 @@
    $bdb = nil
  •  assert_kind_of(BDB::Queue, BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  assert_kind_of(BDB::Queue, unknown = BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  unknown.close
    
    end
    — tests/recno.rb.orig Sun Mar 3 23:23:12 2002
    +++ tests/recno.rb Sun Mar 17 21:21:28 2002
    @@ -192,3 +192,4 @@
    $bdb = nil
  •  assert_kind_of(BDB::Recno, BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  assert_kind_of(BDB::Recno, unknown = BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  unknown.close
    
    end
···


eban

Dossy wrote:

···

On 2002.07.09, Mark Probert <probertm@nortelnetworks.com> wrote:
> sth = dbh.prepare("select au_lname, au_fname from authors where
> state = #{state}")
[...]

You forgot the single-quotes around the string literal in your SQL:

Thank you so much!

Sometimes the simple mistakes are the hardest to find ...

-mark.
---------------------
Mark Probert (probertm@nortelnnetworks.com)
Nortel Networks -- Optera Metro 3000 GNPS
Phone: (613) 768-1082 [ESN: 398-1082]

Also note you can use ?'s as placeholders, then pass the literal to the
execute or as an extra argument to prepare; it’ll quote it for you
taking into account any oddities of the server you’re using.

···

sth = dbh.prepare(“select au_lname, au_fname from authors where state =
"#{state}"”)


Thomas ‘Freaky’ Hurst - freaky@aagh.net - http://www.aagh.net/

Pudder’s Law:
Anything that begins well will end badly.
(Note: The converse of Pudder’s law is not true.)

Better perhaps to try it in Ruby awhile
fbefore making an RCR:

class String
def chars
self.split(“”)
end
def each_char
self.each_byte {|b| yield b.chr }
end
end

Hal Fulton

···

----- Original Message -----
From: “Tobias Reif” tobiasreif@pinkjuice.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, July 08, 2002 2:41 PM
Subject: Re: is there a better string.each?

so what about a simple RCR, asking for

String#chars
, so that we can do
‘foo’.chars #> [“f”,“o”,“o”]
and
‘foo’.chars.each do |char|
perhaps plus String#each_char?

I think that this could work. I also think that an RCR should be
done so that $/ (and the argument to String#each) can be a regexp.

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.07.08 at 15.51.04

···

On Tue, 9 Jul 2002 04:41:04 +0900, Tobias Reif wrote:

Tom Sawyer wrote:

but that left us with no equivalent to the fictional #each_chr,
so what about a simple RCR, asking for
String#chars
so that we can do
‘foo’.chars #> [“f”,“o”,“o”]
and
‘foo’.chars.each do |char|
perhaps plus String#each_char?

tests/recno.rb now works
but

tests/btree.rb
tests/hash.rb
tests/queue.rb
tests/marshal.rb

Still do not.
All tests fails during test_N_env test execution.

Sergei

···

tests/recno.rb:203:in `open’(TestRecno): Permission denied (BDB::Fatal)

The problem is here, try this modification

pigeon% diff -u recno.rb~ recno.rb
— recno.rb~ Sun Mar 3 15:23:12 2002
+++ recno.rb Mon Jul 8 18:30:03 2002
@@ -194,6 +194,7 @@
end
=end
def test_12_env

  •  $bdb.close
     Dir.foreach('tmp') do |x|
       if FileTest.file?("tmp/#{x}")
          File.unlink("tmp/#{x}")
    

pigeon%

Guy Decoux

Tahnk you very much, now all tests are ok except tests/marshal.rb.

Here is the output:

FAILURES!!!
Test Results:
Run: 18/18(113 asserts) Failures: 0 Errors: 7
Errors: 7
tests/marshal.rb:187:in test_11_unknown'(TestBtree): undefined local variable o r method unknown’ for #TestBtree:0x100d0848 (NameError)
from tests/marshal.rb:299

Sergei

http://www.ruby-lang.org/~eban/ruby/binaries/cygwin/ext/bdb-0.3.1-i386-cygwi
n.tar.gz

···

From: “WATANABE Hirofumi” eban@os.rim.or.jp

Try this:

Hi,

class String
def each_char(&block)
self.scan(/./, &block)
end
end

Multibyte version.

···

At Tue, 9 Jul 2002 04:49:22 +0900, Hal E. Fulton hal9000@hypermetrics.com wrote:


Nobu Nakada

if/when $/ accepts regexps, what about ‘’ as a seperator meaning each
character instead of multiple successive \n’s? to me this seems a
dubious equality --an exception caused by the lack of $/ accepting
regexps. if so, each_char (each_chr?) would technically be unneccessary,
but i wouldn’t mind it being there too, along side each_byte and
each_line.

also i don’t know if we need two seperate methods (e.g. #each_line vs.
#lines) for iterating over a block versus returning an array. the same
method can be used for both, depending on whether a block is given or
not. right?

~transami

···

On Mon, 2002-07-08 at 13:52, Austin Ziegler wrote:

On Tue, 9 Jul 2002 04:41:04 +0900, Tobias Reif wrote:

Tom Sawyer wrote:

but that left us with no equivalent to the fictional #each_chr,
so what about a simple RCR, asking for
String#chars
so that we can do
‘foo’.chars #> [“f”,“o”,“o”]
and
‘foo’.chars.each do |char|
perhaps plus String#each_char?

I think that this could work. I also think that an RCR should be
done so that $/ (and the argument to String#each) can be a regexp.

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.07.08 at 15.51.04


~transami

“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin

Hal E. Fulton wrote:

Better perhaps to try it in Ruby awhile
fbefore making an RCR:

class String
def chars
self.split(“”)
end
def each_char
self.each_byte {|b| yield b.chr }
end
end

As I’ve said before, this is a nice solution, Hal. However, I would expect
that native support for this would be faster, and when one is itterating
over characters, speed can quickly become an issue. I was hoping for
something faster than split(‘’), which is also not very memory efficient,
creating an arbitrary Array object in the process.

— SER

Hi,

“Olonichev Sergei” olonichev@scnsoft.com writes:

:Tahnk you very much, now all tests are ok except tests/marshal.rb.
:
:Here is the output:
:…
:FAILURES!!!
:Test Results:
: Run: 18/18(113 asserts) Failures: 0 Errors: 7
:Errors: 7
:tests/marshal.rb:187:in test_11_unknown'(TestBtree): undefined local :variable o :r methodunknown’ for #TestBtree:0x100d0848 (NameError)
: from tests/marshal.rb:299
:…

Sorry for the typo.

Try this again:

diff -u1 tests.orig/btree.rb tests/btree.rb
— tests.orig/btree.rb 2002-03-16 22:36:05.000000000 +0900
+++ tests/btree.rb 2002-07-09 15:37:14.000000000 +0900
@@ -224,2 +224,3 @@
assert_equal(“KLMTU”, $bdb[“yellow”], “”)

  •  $bdb.close
    
    end
    @@ -247,3 +248,4 @@
    $bdb = nil
  •  assert_kind_of(BDB::Btree, BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  assert_kind_of(BDB::Btree, unknown = BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  unknown.close
    
    end
    @@ -313,2 +315,3 @@
    assert_equal(nil, $bdb.close, “”)
  •  $env.close
    
    end
    @@ -378,3 +381,3 @@
    $hash.each_key do |k|
  •   assert($bdb.key?(k), "<key>")
    
  •   assert(!!$bdb.key?(k), "<key>")
    
    end
    diff -u1 tests.orig/hash.rb tests/hash.rb
    — tests.orig/hash.rb 2002-03-16 22:39:50.000000000 +0900
    +++ tests/hash.rb 2002-07-09 15:37:14.000000000 +0900
    @@ -215,3 +215,4 @@
    $bdb = nil
  •  assert_kind_of(BDB::Hash, BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  assert_kind_of(BDB::Hash, unknown = BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  unknown.close
    
    end
    @@ -281,2 +282,4 @@
    def test_17_hash_delete
  •  assert_equal(nil, $bdb.close, "<close>")
    
  •  $env.close
     clean
    

@@ -343,3 +346,3 @@
$hash.each_key do |k|

  •   assert($bdb.key?(k), "<key>")
    
  •   assert(!!$bdb.key?(k), "<key>")
    
    end
    diff -u1 tests.orig/marshal.rb tests/marshal.rb
    — tests.orig/marshal.rb 2002-03-16 22:42:29.000000000 +0900
    +++ tests/marshal.rb 2002-07-09 15:38:34.000000000 +0900
    @@ -185,3 +185,4 @@
    $bdb = nil
  •  assert_kind_of(BDB::Btree, BDB::Unknown.open("tmp/aa", nil, "r", "marshal" => Marshal), "<unknown>")
    
  •  assert_kind_of(BDB::Btree, unknown = BDB::Unknown.open("tmp/aa", nil, "r", "marshal" => Marshal), "<unknown>")
    
  •  unknown.close
    
    end
    diff -u1 tests.orig/queue.rb tests/queue.rb
    — tests.orig/queue.rb 2002-03-03 23:21:12.000000000 +0900
    +++ tests/queue.rb 2002-07-09 15:37:14.000000000 +0900
    @@ -168,3 +168,4 @@
    $bdb = nil
  •  assert_kind_of(BDB::Queue, BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  assert_kind_of(BDB::Queue, unknown = BDB::Unknown.open("tmp/aa", nil, "r"), "<unknown>")
    
  •  unknown.close
    
    end
    diff -u1 tests.orig/recno.rb tests/recno.rb
    — tests.orig/recno.rb 2002-03-03 23:23:12.000000000 +0900
    +++ tests/recno.rb 2002-07-09 15:48:09.000000000 +0900
    @@ -196,2 +196,3 @@
    def test_12_env
  •  $bdb.close
     Dir.foreach('tmp') do |x|