Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Non-Hard Coded File.open(newFile)

Reply
Thread Tools

Non-Hard Coded File.open(newFile)

 
 
Grant Curell
Guest
Posts: n/a
 
      10-23-2010
Like a lot of people I'm new to Ruby and I'm trying to do something I
thought would be pretty simple. I want the user to give me input in the
form of a filename and then subsequently open the file. Here is what I
have so far:

class FileHandler

def initialize()
@fileToParse = fileToParse #I get user input in the form of gets in
#another class.
if File.new(fileToParse)
puts 'File successfully opened.'
else
puts 'File failed to open.'
Kernel.exit

end
end
end

However, that won't work. Doing something like
File.new("C:/Users/grant/Desktop/test.txt") works just fine though. The
way I'm currently doing it throws the following error:

F:/Programming/eclipseWorkspace/CSE_655/file_handler.rb:8:in
`initialize': Invalid argument - C:/Users/grant/Desktop/test.txt
(Errno::EINVAL)

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Hassan Schroeder
Guest
Posts: n/a
 
      10-23-2010
On Sat, Oct 23, 2010 at 6:35 AM, Grant Curell <(E-Mail Removed)> wrote=
:
> Like a lot of people I'm new to Ruby and I'm trying to do something I
> thought would be pretty simple. I want the user to give me input in the
> form of a filename and then subsequently open the file. Here is what I
> have so far:


> =A0def initialize()
> =A0 =A0@fileToParse =3D fileToParse #I get user input in the form of gets=

in
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #another clas=

s.

Are you removing the newline at the end of the string from `gets` ?

> F:/Programming/eclipseWorkspace/CSE_655/file_handler.rb:8:in
> `initialize': Invalid argument - C:/Users/grant/Desktop/test.txt


Probably actually "C:/Users/grant/Desktop/test.txt\n" right here ^

> (Errno::EINVAL)


HTH,
--=20
Hassan Schroeder ------------------------ http://www.velocityreviews.com/forums/(E-Mail Removed)
twitter: @hassan

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      10-23-2010
On Saturday 23 October 2010, Grant Curell wrote:
> |Like a lot of people I'm new to Ruby and I'm trying to do something I
> |thought would be pretty simple. I want the user to give me input in the
> |form of a filename and then subsequently open the file. Here is what I
> |have so far:
> |
> |class FileHandler
> |
> | def initialize()
> | @fileToParse = fileToParse #I get user input in the form of gets in
> | #another class.
> | if File.new(fileToParse)
> | puts 'File successfully opened.'
> | else
> | puts 'File failed to open.'
> | Kernel.exit
> |
> | end
> | end
> |end
> |
> |However, that won't work. Doing something like
> |File.new("C:/Users/grant/Desktop/test.txt") works just fine though. The
> |way I'm currently doing it throws the following error:
> |
> |F:/Programming/eclipseWorkspace/CSE_655/file_handler.rb:8:in
> |`initialize': Invalid argument - C:/Users/grant/Desktop/test.txt
> |(Errno::EINVAL)


Your code seems mostly correct, but I don't exactly understand what
fileToParse is. Is it a call to a fileToParse method (which in turn uses geets
to retrieve the file name) or a variable? In the first case, I guess the if
line should be

if File.new(@fileToParse) #note the @

Otherwise, the user will be asked to enter the file name twice.

If fileToParse is a variable, instead, where does it come from?


Also, it would be useful if you pointed out which is line 8 on your program.
If I assume the first line is

class FileHandler

then line 8 is 'else', which I doubt could give you such an error.

Stefano

 
Reply With Quote
 
Grant Curell
Guest
Posts: n/a
 
      10-23-2010
Thank you so much Hassan, that fix seemed so obvious after you said it
lol. I just changed line 8 to if File.new(fileToParse.delete "\n") and
it worked just fine. Not sure if I should ask this here but just out of
curiosity does the debugger for 1.9.2 not work? I'm running the eclipse
plug in right now and whenever I try to debug it just vomits all over
itself. Browsed around the web and saw it mentioned a couple of times.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Hassan Schroeder
Guest
Posts: n/a
 
      10-23-2010
On Sat, Oct 23, 2010 at 8:17 AM, Grant Curell <(E-Mail Removed)> wrote:
> Thank you so much Hassan, that fix seemed so obvious after you said it
> lol. I just changed line 8 to if File.new(fileToParse.delete "\n")


Good, but you should probably look at fileToParse.chomp for a more
platform-agnostic approach

> Not sure if I should ask this here but just out of
> curiosity does the debugger for 1.9.2 not work? I'm running the eclipse
> plug in right now


Sorry, no idea about Eclipse.

--
Hassan Schroeder ------------------------ (E-Mail Removed)
twitter: @hassan

 
Reply With Quote
 
w_a_x_man
Guest
Posts: n/a
 
      10-23-2010
On Oct 23, 8:35*am, Grant Curell <(E-Mail Removed)> wrote:
> Like a lot of people I'm new to Ruby and I'm trying to do something I
> thought would be pretty simple. I want the user to give me input in the
> form of a filename and then subsequently open the file. Here is what I
> have so far:
>
> class FileHandler
>
> * def initialize()
> * * @fileToParse = fileToParse #I get user input in the form of gets in


Usually after gets you want to use strip to remove all leading
and trailing whitespace:

file_name = gets.strip
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If MS coded vi Blinky the Shark HTML 1 04-27-2006 12:44 PM
must to remove hard coded https links from aspx and ascx pages Luis Esteban Valencia ASP .Net 1 01-06-2005 03:25 PM
Binary-Coded Decimal conversion kelvSYC Java 2 12-30-2003 04:06 PM
eliminate hard-coded file names in xsl Luke Airig XML 1 12-24-2003 11:11 AM
Colour coded SELECT Otuatail HTML 6 12-19-2003 12:22 PM



Advertisments