I get "'initialize' no such file or directory error" when I try to run the following code:
(taken from 'Learn Ruby The Hard Way')
#Code start
filename=ARGV.first
txt=File.open(filename)
puts "Here is your file: #{filename}"
puts txt.read()
puts "Type it again"
file_again=STDIN.gets.chomp()
txt_again=File.open(file_again)
puts txt_again.read()
#Code end
Both of the ruby file and the txt file are in the same directory, even then it gives the error.
This is the error I get in cmd:
fileread.rb ex.txt
Here is your file: ex.txt
type it again
hello world
C:/Documents and Settings/Administrator/fileread.rb:8:in `initialize': No such f
ile or directory - hello world (Errno::ENOENT)
from C:/Documents and Settings/Administrator/fileread.rb:8:in `open'
from C:/Documents and Settings/Administrator/fileread.rb:8:in main
You're supposed to type ex.txt when it asks you to type it again.
Related
I tried simple ruby script to print name. I used notepad to code and command prompt to display the output and to get input from user.
My code in notepad (hello.rb) :
def hello(name)
puts "hello,#{name}"
end
puts "enter name"
name=gets.chomp
puts hello(name)
when i tried getting input from user,i got an error
`gets': Invalid argument - <STDIN> (Errno::EINVAL) from hello.rb:6:in `gets' from hello.rb:6:in `<main>'
but when i tried it in online compiler its working fine. how to resolve this? Thanks is advance!
So, I'm relatively new to programming, and I have started working with ruby. I am going through "Learn how to code the hard way: Ruby" and I am on exercise 15; the beginning of file reading. I have copied the code they provided word for word, literally copy and pasted it to make sure, but I am getting the same error. I've googled the error, but to no avail. I have the .rb file in the same directory as the .txt file I'm trying to read. Here is my code.
filename = ARGV.first
prompt = "> "
txt = File.open(filename)
puts "Here's your file: #{filename}"
puts txt.read()
puts "I'll also ask you to type it again:"
print prompt
file_again = STDIN.gets.chomp()
txt_again = File.open(file_again)
puts txt_again.read()
The error I keep getting it this:
ex15.rb:19:in 'initialize': No such file of directory - ex15.txt <Errno::ENOENT>
from ex15.rb:4:in 'open'
from ex15.rb:4:in '<main>'
command to run it:
ruby ex15.rb ex15.txt
Any help is appreciated. Thanks
When you don't specify the mode argument for File.open(), the default is 'r', which stands for read. And to read a file, it has to exist already. The error message is telling you that there is no file named 'ex15.txt' in the current directory for ruby to read.
To get rid of the error, create a file called ex15.txt in the current directory, and type 'hello world' in the file.
I'm following a tutorial to create ruby gems http://guides.rubygems.org/make-your-own-gem/
The tutorial tells me to create a ruby file like this:
% cat lib/hola.rb
class Hola
def self.hi
puts "Hello world!"
end
end
Then a gemspec file like this:
% cat hola.gemspec
Gem::Specification.new do |s|
s.name = 'hola'
s.version = '0.0.0'
s.date = '2010-04-28'
s.summary = "Hola!"
s.description = "A simple hello world gem"
s.authors = ["Nick Quaranto"]
s.email = 'nick#quaran.to'
s.files = ["lib/hola.rb"]
s.homepage =
'http://rubygems.org/gems/hola'
end
When I gem build hola.gemspec I get this error:
Invalid gemspec in [hola.gemspec]: hola.gemspec:1: syntax error, unexpected tIDENTIFIER, expecting $end
% cat hola.gemspec
^
ERROR: Error loading gemspec. Aborting.
Now his code on Github will not build without the Rakefile.
So how can I make this work? Do I need to add a Rakefile or is there something wrong with the code?
Your error indicates that your file has the line % cat hola.gemspec in it literally. This line in the example isn't intended to be part of the file itself; it's the Unix command the author used to print the contents of the file. Remove that line and the similar line from the other file and you should be OK to move to the next step.
The first line, % cat lib/hola.rb is not meant to be part of the file, but rather the whole thing is command-line output. cat is a command used to output the contents of a file, and things like % and $ are often used to denote the start of a command. So, remove the first line from the file.
While trying to upload a file in Ruby on Rails, I ran into an issue.
Here is how I upload a file:
def upload_image(image)
File.new(Rails.root.join('assets','images','products',image.original_filename),'wb') do |f|
f.write(image.read)
end
end
Which throws an exception:
Errno::ENOENT in ProductsController#update
No such file or directory - /home/alex/RubymineProjects/psg/assets/images/products/my-image.png
Why is this happening? I'm just creating a new file, I'm not trying to open an existing one.
It does not create directories.
File.new("test", 'wb') #=> creates the file test
File.new("test/test", 'wb') #=> test.rb:1:in `initialize': No such file or directory - test/test (Errno::ENOENT)
If you add an /app you have the path you are looking for. Don't really think thats the way to use the asset pipeline though. See reasoning in this question.
File.open(Rails.root.join('app','assets','images','test.jpg'),'wb') do |f|
f.write("image")
end
=> 5
cat app/assets/images/test.jpg #=> image%
I want to open file and read it that I pass from console.
Like
filename = gets()
File.open(filename,'r') do |file|
but getting error like following on console:
test.rb:7:in `initialize': Invalid argument - myfile (Errno::EINVAL)
from test.rb:7:in `open'
from test.rb:7
Is it possible to read file having filename taken from console and perform do |file| ..end in ruby?
String you read from the STDIN has a trailing \n. Get rid of it.
filename = gets().chomp