Ruby libraries

A

Andrew Falanga

Hi,

New to Ruby and I'm not having much luck finding the answer to this
question at this time. Are there Ruby equivalents to the TWAPI
extension library for TCL? I need to be able to "mount" a share from a
script for Windows. I would also like to know if there is an FTP
library that can be used in Ruby.

Currently, I'm using TCL, but I need to have threading support and to my
pleasant surprise, Ruby supports threading natively. Plus, the language
thus far, looks to be much more "fun" to work with than TCL.

Thanks in advance for any help provided.
 
E

Ezra Zygmuntowicz

Andrew-
There is an ftp library in the ruby standard library. Here is a
little script that recursively descends through a directory and
uploads all the files to an ftp server:

#!/usr/local/bin/ruby
# This script uploads a folder of images via ftp

require 'find'
require 'net/ftp'

path = "/Volumes/Users/ez/logos/" # <= set this to the path of your
folder of images

ftp = Net::FTP.new('ftp.example.com')
ftp.login("username", "password")
files = ftp.chdir('logos')

Find.find(path) do |file|
unless test(?d, file) #<= this tests to make sure its a
file and not a directory before continuing.
ftp.putbinaryfile("#{file}", "#{File.basename(file)}") #<=
this assumes binary files.
puts "Uploaded #{file}"
end
end

ftp.close

Hope that helps
-Ezra

Hi,

New to Ruby and I'm not having much luck finding the answer to this
question at this time. Are there Ruby equivalents to the TWAPI
extension library for TCL? I need to be able to "mount" a share
from a script for Windows. I would also like to know if there is
an FTP library that can be used in Ruby.

Currently, I'm using TCL, but I need to have threading support and
to my pleasant surprise, Ruby supports threading natively. Plus,
the language thus far, looks to be much more "fun" to work with
than TCL.

Thanks in advance for any help provided.

---------------------------------------------
Andrew R. Falanga (a non-HP employee)
Hewlett-Packard Company
11311 Chinden Blvd.
Boise, Idaho

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
(e-mail address removed)
 
A

Andrew Falanga

Ezra said:
Andrew-
There is an ftp library in the ruby standard library. Here is a
little script that recursively descends through a directory and uploads
all the files to an ftp server:

#!/usr/local/bin/ruby
# This script uploads a folder of images via ftp

require 'find'
require 'net/ftp'

path = "/Volumes/Users/ez/logos/" # <= set this to the path of your
folder of images

ftp = Net::FTP.new('ftp.example.com')
ftp.login("username", "password")
files = ftp.chdir('logos')

Find.find(path) do |file|
unless test(?d, file) #<= this tests to make sure its a file
and not a directory before continuing.
ftp.putbinaryfile("#{file}", "#{File.basename(file)}") #<=
this assumes binary files.
puts "Uploaded #{file}"
end
end

ftp.close

Hope that helps
-Ezra

Outstanding! I forgot to ask in the original post, however, that I also
need a library for creating CRC's with a divisor of 32bits. Does
something like this exist?
 
N

NAKAMURA, Hiroshi

--------------enig864F2775BF31181C968CAABB
Content-Type: multipart/mixed;
boundary="------------010001060308060406090600"

This is a multi-part message in MIME format.
--------------010001060308060406090600
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Hi,

Andrew said:
Outstanding! I forgot to ask in the original post, however, that I also
need a library for creating CRC's with a divisor of 32bits. Does
something like this exist?

You can use Zlib.crc32 if you need CRC32-Reversed. And attached is my
port of CRC base functions in Classless.hasher at
http://www.classless.net/projects/hasher/ . Use this if you need CRC32
- not Reversed.

Regards,
// NaHi

--------------010001060308060406090600
Content-Type: text/plain;
name="crc.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="crc.rb"

# crc.rb - Ruby port of CRC.cs in Classless.Hasher - C#/.NET Hash and Checksum Algorithm Library. (http://www.classless.net/projects/hasher/)

=begin
following is an excerpt of CRC.cs in Classless.Hasher.

/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Classless.Hasher - C#/.NET Hash and Checksum Algorithm Library.
*
* The Initial Developer of the Original Code is Classless.net.
* Portions created by the Initial Developer are Copyright (C) 2004 the Initial
* Developer. All Rights Reserved.
*
* Contributor(s):
* Jason Simeone ([email protected])
*
* ***** END LICENSE BLOCK ***** */
=end


class CRC
def initialize(order, poly, iv, xor, reflect)
@order = order
@poly = poly
@iv = iv
@xor = xor
@reflect = reflect
if @reflect
@mask = (2 ** (@order - 8)) - 1
end
@lookup = build_lookup
@crc = init
end

def init
crc = @iv
if @reflect
crc = reflect(crc, @order)
end
crc
end

def update(str)
topbit = 1 << (@order - 1)
widthmask = (topbit << 1) - 1
str.each_byte do |byte|
if @reflect
@crc = ((@crc >> 8) & @mask) ^ @lookup[(@crc ^ byte) & 0xFF]
else
@crc = (@crc << 8) ^ @lookup[((@crc >> (@order - 8)) ^ byte) & 0xFF]
end
@crc &= widthmask
end
end

def final
@crc ^= @xor
bytes = (@order + 7) >> 3
@crc
end

private

def build_lookup
lookup = []
topbit = 1 << (@order - 1)
widthmask = (topbit << 1) - 1
for idx in 0..255
v = idx
v = reflect(v, 8) if @reflect
v <<= (@order - 8)
8.times do
if (v & topbit).nonzero?
v = (v << 1) ^ @poly
else
v <<= 1
end
end
v = reflect(v, @order) if @reflect
v &= widthmask
lookup << v
end
lookup
end

def reflect(crc, bits)
base = crc
for idx in 0...bits
bitmask = 1 << ((bits - 1) - idx)
if (base & 1).nonzero?
crc |= bitmask;
else
crc &= ~bitmask;
end
base >>= 1
end
crc
end
end


if __FILE__ == $0
crc32 = CRC.new(32, 0xEDB88320, 0xFFFFFFFF, 0xFFFFFFFF, false)
crc32rev = CRC.new(32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true)
crc32.update("12345")
p crc32.final
crc32rev.update("12345")
p crc32rev.final

require 'zlib'
p Zlib.crc32("12345")

crc8 = CRC.new(8, 0xE0, 0x00, 0x00, false)
msg = ARGV.shift
msg = "\1\2\3\4"
crc8.update(msg)
printf("%x\n", crc8.final)

require 'pgp/util'

crc24 = CRC.new(24, 0x01864cfb, 0x00b704ce, 0, false)

msg = ARGV.shift
crc24.update(msg)
p crc24.final

p PGP::Util.crc24(msg)
end

--------------010001060308060406090600--

--------------enig864F2775BF31181C968CAABB
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Cygwin)

iD8DBQFC6Yp5f6b33ts2dPkRAjxeAKCQRZMSG2NwE1lrl3BG6gyFcX3T/ACgypyW
TdXCG+Uf80nwW/77tU9Ahfw=
=kRCL
-----END PGP SIGNATURE-----

--------------enig864F2775BF31181C968CAABB--
 
A

Alexandru Popescu

#: by Ezra Zygmuntowicz's words the mind was *winged* :#
Andrew-
There is an ftp library in the ruby standard library. Here is a
little script that recursively descends through a directory and
uploads all the files to an ftp server:

#!/usr/local/bin/ruby
# This script uploads a folder of images via ftp

require 'find'
require 'net/ftp'

path = "/Volumes/Users/ez/logos/" # <= set this to the path of your
folder of images

ftp = Net::FTP.new('ftp.example.com')
ftp.login("username", "password")
files = ftp.chdir('logos')

Find.find(path) do |file|
unless test(?d, file) #<= this tests to make sure its a
file and not a directory before continuing.
ftp.putbinaryfile("#{file}", "#{File.basename(file)}") #<=
this assumes binary files.
puts "Uploaded #{file}"
end
end

ftp.close

Hope that helps
-Ezra



-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
(e-mail address removed)
Wouldn't be correct to have the close() call in a finally block? (so to be assured that it always run)

:alex |.::the_mindstorm::.|
 
E

Ezra Zygmuntowicz

#: by Ezra Zygmuntowicz's words the mind was *winged* :#

Wouldn't be correct to have the close() call in a finally block?
(so to be assured that it always run)

:alex |.::the_mindstorm::.|
Sure, I was just throwing up a quick example. If it's code you would
be using a lot definitely put it in a begin ..rescue...ensure..end
block.
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
(e-mail address removed)
509-577-7732
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top