Re: ruby woes

Top Page
Attachments:
Message as email
+ (text/plain)
+ (text/html)
+ (text/plain)
Delete this message
Reply to this message
Author: Lisa Kachold
Date:  
To: Main PLUG discussion list
Subject: Re: ruby woes
Hi Mike,

This script appears to require the "filename" passed to the command line
when invoked?

#./script /tmp/filename

variable becomes ARGV.first {filename}

Be sure you have the right filename and complete path.

On Wed, Dec 28, 2011 at 10:56 PM, Michael Havens <> wrote:

> I hate it when you are trying to learn something and what you are
> learning from is incomplete or wrong. This is the beginning of a ruby
> script I am writing which will (in the section shown) erase a file. ruby
> tells me that line 15 is wrong! It says:
>
>     ex16.rb:15: undefined local variable or method `file' for main:Object
> (NameError)

>
> I am guessing that what line 15 is doing is telling the program to make
> file 'target' smaller but it needs to know how small and the guy who writes
> Learn Code the Hard Way forgot to include that in his tutorial. Or else I'm
> wrong and in that case please show me where I am wrong.
>
> 1 filename = ARGV.first #argument at command line is given a name
> 2 script = $0 #script is now titled file's name
> 3
> 4 puts "We're going to erase the contents of #{filename}."
> 5 puts "If you don't want to do that hit CTRL-C."
> 6 puts "If you do want to do that then hit RETURN."
> 7
> 8 print "? "
> 9 STDIN.gets #it waits for input before going on
> 10
> 11 puts "Opening the file..."
> 12 target = File.open(filename, 'w') #tells it to open filename or to
> write it if non-existant
> 13
> ^^^^is this right?^^^^^^^^^^
> 14 puts "Truncating the file. Goodbye!"
> 15 target.truncate(target.size)
>
>
> What version of Ruby do you have?


In ruby 1.8.7 it works as documented in the Pick Axe book (only class
method on File, no instance method). File.size(target) calls the
*class method* which is
defined for File class, while target.size calls an *instance method*
which is not defined for File class.


Try this:

flength=File.size(target)
target.truncate(flength)


*Full Script:
*
filename = ARGV.first

script = $0



puts "We're going to erase #{filename}."

puts "If you don't want that, hit CTRL-C (^C)."

puts "If you do want that, hit RETURN."



print "? "

STDIN.gets



puts "Opening the file..."

target = File.open(filename, 'w')



puts "Truncating the file. Goodbye!"

flength=File.size(target)
target.truncate(flength)



puts "Now I'm going to ask you for three lines."



print "line 1: "; line1 = STDIN.gets.chomp()

print "line 2: "; line2 = STDIN.gets.chomp()

print "line 3: "; line3 = STDIN.gets.chomp()



puts "I'm going to write these to the file."



target.write(line1)

target.write("\n")

target.write(line2)

target.write("\n")

target.write(line3)

target.write("\n")



puts "And finally, we close it."

target.close()




> --
> :-)~MIKE~(-:
>
> ---------------------------------------------------
> PLUG-discuss mailing list -
> To subscribe, unsubscribe, or to change your mail settings:
> http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss
>


--
(602) 791-8002 Android
(623) 239-3392 Skype
(623) 688-3392 Google Voice
**
HomeSmartInternational.com
---------------------------------------------------
PLUG-discuss mailing list -
To subscribe, unsubscribe, or to change your mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss