# Net::FTP.new silently failing in block form

**URL:** <https://www.ruby-forum.com/t/net-ftp-new-silently-failing-in-block-form/71659>\
**Category:** Ruby\
**Created:** [October 10, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/net-ftp-new-silently-failing-in-block-form/71659 "2006-10-10T16:37:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [October 10, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/net-ftp-new-silently-failing-in-block-form/71659/1 "2006-10-10T16:37:20Z")

</div>

Hi all,

Ruby 1.8.5

I’m hitting some odd behavior with Net::FTP.new. I’m connecting to a  
Win2k3 server (running IIS 6, if that matters). The directory style is  
set to “UNIX”, and the ftp server is in passive mode.

I’m trying to “put” a simple text file. When I use the block form of  
FTP.new nothing happens. No error, but the text file is not ftp’d to  
the remote machine. When I use the non-block form, everything works  
fine.

Sampe script:

# This fails

require “net/ftp”  
include Net

file = “test.txt”

FTP.new(host){ |ftp|  
ftp.passive = true  
ftp.login(user, pass)  
ftp.chdir(dir)  
ftp.put(file)  
}

However, this snippet works just fine:

ftp = FTP.new(host)  
ftp.passive = true  
ftp.login(user, pass)  
ftp.chdir(dir)  
ftp.put(file)  
ftp.close

Note that the “ftp.passive = true” had no effect one way or another.  
Looking at the source code for Net::FTP revealed nothing out of the  
ordinary. I ran the same script from both a Linux machine and a  
Windows XP box and got the same behavior, so I don’t think it’s a line  
ending issue.

Has anyone seen this before?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [October 10, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/net-ftp-new-silently-failing-in-block-form/71659/2 "2006-10-10T16:39:21Z")

</div>

> > > > > “D” == Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

D\> FTP.new(host){ |ftp|  
^^^  
open

D\> ftp.passive = true

Guy Decoux

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [October 10, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/net-ftp-new-silently-failing-in-block-form/71659/3 "2006-10-10T16:51:25Z")

</div>

ts wrote:

> > > > > > “D” == Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> D\> FTP.new(host){ |ftp|  
> ^^^  
> open
> 
> D\> ftp.passive = true

You’re right, that does work. But, based on the source, why would that  
make a difference? I mean, FTP.open just calls FTP.new internally,  
right? The only difference seems to be that FTP.open requires a host  
name.

Oh, well. I’ll just use FTP.open from now on. 🙂

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [October 10, 2006, 4:53pm UTC](https://www.ruby-forum.com/t/net-ftp-new-silently-failing-in-block-form/71659/4 "2006-10-10T16:53:25Z")

</div>

> > > > > “D” == Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

D\> You’re right, that does work. But, based on the source, why would  
that  
D\> make a difference? I mean, FTP.open just calls FTP.new internally,  
D\> right? The only difference seems to be that FTP.open requires a host  
D\> name.

We don’t have the same source

```
def FTP.open(host, user = nil, passwd = nil, acct = nil)
  if block_given?

```

# ^^^^^^^^^^^^

```
    ftp = new(host, user, passwd, acct)
    begin
      yield ftp

```

# ^^^^^^^^^

```
    ensure
      ftp.close
    end
  else
    new(host, user, passwd, acct)
  end
end

```

Guy Decoux

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [October 10, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/net-ftp-new-silently-failing-in-block-form/71659/5 "2006-10-10T16:51:25Z")

</div>

Daniel B. wrote:

> the remote machine. When I use the non-block form, everything works  
> FTP.new(host){ |ftp|  
> ftp.login(user, pass)  
> ftp.chdir(dir)  
> ftp.put(file)  
> ftp.close
> 
> Note that the “ftp.passive = true” had no effect one way or another.  
> Looking at the source code for Net::FTP revealed nothing out of the  
> ordinary. I ran the same script from both a Linux machine and a  
> Windows XP box and got the same behavior, so I don’t think it’s a line  
> ending issue.

I looked at [ruby-doc.org](http://ruby-doc.org) and it looks like new does not take an optional  
block whereas open does.

Jamey

Confidentiality Notice: This email message, including any attachments,  
is for the sole use of the intended recipient(s) and may contain  
confidential and/or privileged information. If you are not the intended  
recipient(s), you are hereby notified that any dissemination,  
unauthorized review, use, disclosure or distribution of this email and  
any materials contained in any attachments is prohibited. If you receive  
this message in error, or are not the intended recipient(s), please  
immediately notify the sender by email and destroy all copies of the  
original message, including attachments.

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [October 10, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/net-ftp-new-silently-failing-in-block-form/71659/6 "2006-10-10T16:57:26Z")

</div>

Jamey C. wrote:

> > FTP.new nothing happens. No error, but the text file is not ftp’d to
> > 
> > ftp.passive = true
> 
> I looked at [ruby-doc.org](http://ruby-doc.org) and it looks like new does not take an optional  
> block whereas open does.
> 
> Jamey

Oh, you’re right!

/me slaps self.

Thanks Jamey and Guy.

- Dan
