On Sep 29, 2005, at 1:48 PM, Louis J Scoras wrote:

> On 9/29/05, Jamis Buck <jamis / 37signals.com> wrote:
>
>>
>> No, it is correct. open_handle returns an opaque value that
>> represents a remote file (or directory) handle. It just happens to be
>> implemented as a string. To use it, you call sftp.read and
>> sftp.write, passing the handle to it. (You can look at the
>> synchronous.rb file in the examples subdirectory of the net-sftp
>> distro for more examples of how to use open-handle).
>>
>> - Jamis
>>
>>
>>
>>
> Hrmm... I only assumed it was wrong because I got an error when I  
> tried
> sending read/write to the handle:
>
> sftp_t.rb:8: undefined method `read' for "\000\000\000\000":String
> (NoMethodError)
> from sftp_t.rb:5:in `open_handle'
> from sftp_t.rb:5
> from sftp_t.rb:3:in `initialize'
> from /usr/lib/ruby/1.8/net/sftp/session.rb:45:in `call'
> from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:152:in `do_version'
> from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:183:in `do_data'
> from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:19:in `to_proc'
> from /usr/lib/ruby/1.8/net/sftp/protocol/driver.rb:103:in `call'
> from /usr/lib/ruby/1.8/net/ssh/connection/channel.rb:496:in `callback'
> from /usr/lib/ruby/1.8/net/ssh/connection/channel.rb:447:in `do_data'
> from /usr/lib/ruby/1.8/net/ssh/connection/driver.rb:285:in  
> `do_channel_data'
> from /usr/lib/ruby/1.8/net/ssh/connection/driver.rb:151:in `process'
> from /usr/lib/ruby/1.8/net/ssh/connection/driver.rb:134:in `loop'
> from /usr/lib/ruby/1.8/net/ssh/session.rb:161:in `loop'
> from /usr/lib/ruby/1.8/net/sftp/session.rb:66:in `initialize'
> from /usr/lib/ruby/1.8/net/sftp.rb:29:in `start'

Yah, that would be problematic. :) You have to send read/write to the  
sftp instance, and pass the handle to it. Very un-Rubyish, I know:

   sftp.open_handle("foo.txt") do |handle|
     data = sftp.read(handle)
   end

Please do take a look at the examples subdirectory. It will clear up  
a lot of questions about how to use these methods. Likewise, the  
Net::SFTP FAQ may be enlightening: http://net-ssh.rubyforge.org/sftp/ 
faq.html.

- Jamis