Can't read a file that contains a question mark in name - ruby

I have a file with a name something like: /path/to/file/dir with spaces/mytiff-?.tif. In irb I try both the following:
open("/path/to/file/dir with spaces/mytiff-?.tif", 'rb')
File.open("/path/to/file/dir with spaces/mytiff-?.tif", 'rb')
And I'm getting an error:
Errno::ENOENT: No such file or directory.
I've also tried replacing the ? with \? in the string.
I'm using ruby 2.0.0. File is network drive (CIFS), client is a mac (lion).
Update
Thanks for the help. The problem is still not solved.
$ ls "/Volumes/rmrpp/MLP Library/Bridgland 1927 - North Saskatchewan/Stations/420/Repeat Masters/"
HIG2008_B27_420-100.tif HIG2008_B27_420-90.tif HIG2008_B27_420-92.tif HIG2008_B27_420-94.tif HIG2008_B27_420-96.tif HIG2008_B27_420-98.tif HIG2008_B27_420-?.tif
HIG2008_B27_420-101.tif HIG2008_B27_420-91.tif HIG2008_B27_420-93.tif HIG2008_B27_420-95.tif HIG2008_B27_420-97.tif HIG2008_B27_420-99.tif
$ irb
1.9.3p194 :001 > t = File.open("/Volumes/rmrpp/MLP Library/Bridgland 1927 - North Saskatchewan/Stations/420/Repeat Masters/HIG2008_B27_420-?.tif",'rb'); nil
Errno::ENOENT: No such file or directory - /Volumes/rmrpp/MLP Library/Bridgland 1927 - North Saskatchewan/Stations/420/Repeat Masters/HIG2008_B27_420-?.tif
from (irb):1:in `initialize'
from (irb):1:in `open'
from (irb):1
from /Users/mlp/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
1.9.3p194 :002 > t = File.open("/Volumes/rmrpp/MLP Library/Bridgland 1927 - North Saskatchewan/Stations/420/Repeat Masters/HIG2008_B27_420-98.tif",'rb'); nil
=> nil
1.9.3p194 :003 > t.size
=> 50124180
As you can see, the file exists (I'm listing it in bash). I go into irb and try to read the file as a binary. The file path is quoted. It fails. I try another file from the same directory and it works.
I think #mu_is_too_short is correct. Maybe the ? isn't a question mark at all. The only viable solution I can think of is to manually rename the files with the mystery question mark. Any other ideas?

As long as the file exists, there is no problem opening a file like this:
folder_file = './temp directory/file-?.txt'
File.open(folder_file, 'r').close
This works fine.
See Tin Man's suggestion about how to actually open the .tif file.
As you can see, I created the following file myster-file_char?.txt
$: ls
get_listing.rb myster-file_char?.txt
You can get this stored in a variable, though I only use Dir.glob here to view it.
puts Dir.glob("**.*").inspect
Everything is now revealed in what character it is. Though I should be able to use it directly as is to open and edit the file in Ruby. Though like The Tin Man stated, you are asking about a binary file.
$: ruby get_listing.rb
["myster-file_char\u0016.txt", "get_listing.rb"]
The character that is actually there is a CTRLV. Hope this finally helps to unravel the mystery.

Related

Errors with getting file permissions with File.Stat (Errno::ENOENT)

I'm new to programming and I am trying to figure out how to grab all the recursive directories of my array, and get the file permissions of all of them.
Googling was pointing me to file.stat, however the code is stating there is no such file or directory called 'stat'. For context, after getting the file permissions, my next objective is to be able to compare the permissions of the file owner to the group to determine any differences. I think part of my problem displayed with 'File::Stat:0x7f2407a9e908' is that it's not converting to an integer.
Any suggestions on reading material or revision would be greatly appreciated.
Here is my code so far:
%w(/etc /bin /usr/bin /usr/lbin /usr/usb /sbin /usr/sbin).each do |dir|
Dir.glob("#{dir}/**").each do |c|
s = File.stat("#{c}")
puts s
end
end
And here is my return:
#<File::Stat:0x7f2407a9e908>
test.rb:3:in `stat': No such file or directory - /usr/bin/chef-zero (Errno::ENOENT)
from test.rb:3
from test.rb:2:in `each'
from test.rb:2
from test.rb:1:in `each'
from test.rb:1

Require not able to find ruby file

I am an absolute beginner in Ruby. I created a small ruby file, and it runs well when I run the command ruby "methods.rb". That means I am in the correct directory.
But when I launch irb and run the command require "methods.rb", I get the following response:
LoadError: cannot load such file -- methods.rb
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:53:in `require'
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_require.rb:53:in `require'
from (irb):1
from /usr/local/rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
Ruby doesn't add the current path to the load path by default.
From irb, you can try require "./methods.rb" instead.
I do have a ruby file called so.rb in the directory /home/kirti/Ruby. So first from IRB I would change my current working directory using Dir#chdir method. Then I would call #load or #require method. My so.rb file contains only p hello line.
I would go this way :
>> Dir.pwd
=> "/home/kirti"
>> Dir.chdir("/home/kirti/Ruby")
=> 0
>> Dir.pwd
=> "/home/kirti/Ruby"
>> load 'so.rb'
"hello"
=> true
>> require './so.rb'
"hello"
=> true
To add the directory you are executing the ruby script from to the load path use:
$LOAD_PATH.unshift( File.join( File.dirname(__FILE__), '' ) )
or if you have put your dependencies in 'subdir' of the current directory:
$LOAD_PATH.unshift( File.join( File.dirname(__FILE__), 'subdir' ) )
If you are going to load things in IRB that are in your current directory, you can do:
irb -I.
Note the 'dot' there, indicating current directory.
If you are exploring and making changes in that file, while you are in IRB, use load rather than `require as load lets you load your changes, and require will only allow the file to be required once. This means you will not need to exit IRB to see how your changes are being affected.
To find out what options you have for IRB, you can do irb --help which is good to do if you are learning the tool.

Console not ready ruby sublime text file

I'm new to programming. Just about to start learning Ruby. I already took a console class, but I am stuck here.
I'm using a mac 10.6.8. I have done a quick 1+2 in the sublime text editor. I saved it. I went over to my console typed irb and then typed ruby example.rb. I have read elsewhere here that typing require './example' would help....it didn't. I am getting the following
NameError: undefined local variable or method `example' for main:Object
from (irb):2
from /usr/local/rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
I don't understand what I am doing wrong. Thank you for your help. I really appreciate it.
-L
I would do as below:
kirti#kirti-Aspire-5733Z:~$ irb
2.0.0p0 :001 > require 'fileutils'
=> true
2.0.0p0 :002 > FileUtils.pwd
=> "/home/kirti"
2.0.0p0 :003 > FileUtils.cd "/home/kirti/ruby"
=> nil
2.0.0p0 :004 > load "./SO.rb"
3
=> true
2.0.0p0 :005 > require "./SO.rb"
3
=> true
My SO.rb file contains the below line :
puts 1+2
May be you wanna give a try.
Step 1: Navigate to your project/file folder by using command "cd folder_name/folder_location"
Step 2: load './example.rb'
For better solution you may wanna define some function inside example.rb
Like:
def sum
1 + 2
end
And to get the output enter sum in irb after loading the example.rb file.
irb is the interactive ruby shell. Within the shell, everything you type is interpreted as Ruby code, not bash commands. So, for example:
bash> puts 1 + 2
# command not found: puts
# this happens because you're not in a Ruby shell
bash> irb
# now you're in a Ruby shell
irb> puts 1 + 2
# 3
If you wrote some code in example.rb, you have two options:
From the bash shell, run ruby example.rb (from the same directory where your example.rb file is saved.
From the irb console, you can require 'example', which will load the contents of example.rb into your interpreter. In this case, it will immediately execute the Ruby code. If you wrapped the contents of example.rb in a class, it would load the class, but not execute code within it until you instantiated/called it.
Hopefully that helps!
My guess is that you are typing (into irb):
require example.rb
When you need to type:
require './example.rb'
The first tells ruby: "require what is in a variable called example". Because you did not define a variable called example, it results in the no variable or method error.
The second tells ruby: "require a string './example.rb'". Since the require method essentially knows how to find the file name passed as a string and evaluate the file, you'll get the right output
By the way, for this example, example.rb needs to be in the same directory. If example.rb is in another directory, you'll need to use the full path (I won't expand on it here) to source it.
You'll also notice that the output will look something like this:
3
=> true
This is because the file was evaluated (executing the code: puts 1+2) and the require method returns true to indicate it evaluated the file.
If you require the file again, you'll get false because the file is already loaded.

Neither ruby and nor irb can load .rb file in current directory

I'm having a really noob problem with importing files in Ruby. I'm making a Ruby app in Windows XP. All the class files for the app are in "C:/Documents/Prgm/Surveyor_Ruby/lib". But when I require a file in another file, neither ruby nor irb can find the required file.
The current directory's contents:
C:\Documents\Prgm\Surveyor_Ruby\lib>dir
Volume in drive C has no label.
Volume Serial Number is AAAA-BBBB
Directory of C:\Documents\Prgm\Surveyor_Ruby\lib
10/09/2010 06:32 PM <DIR> .
10/09/2010 06:32 PM <DIR> ..
10/08/2010 03:22 PM 5,462 main (commented).rb
10/08/2010 03:41 PM 92 question.rb
10/08/2010 09:06 PM 2,809 survey.rb
10/09/2010 06:25 PM 661 surveyor.rb
10/08/2010 01:39 PM 1,546 test.rb
5 File(s) 10,570 bytes
2 Dir(s) 40,255,045,632 bytes free
Confirmation that irb is in correct directory:
C:\Documents\Prgm\Surveyor_Ruby\lib>irb
irb(main):001:0> Dir.pwd
=> "C:/Documents/Prgm/Surveyor_Ruby/lib"
...yet irb can't load survey.rb:
irb(main):002:0> require 'survey'
LoadError: no such file to load -- survey
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from (irb):2
from C:/Ruby192/bin/irb:12:in `<main>'
None of these worked for me, but this did:
irb -I .
>require 'file'
=> true
require './hede'
or
require_relative 'hede'
This works for me in both Ruby (1.9.3) and JRuby (1.7.x) on linux. I haven't tested it on windows.
How about this command? A little cumbersome to write but really clean and it should always work:
➜ $ irb
> require "#{Dir.pwd}/file_to_load.rb"
=> true
Noticed the same behavior but my linux roots had me try:.\file.rb and it loaded into the irb. Try explicitly declaring the current directory.
it's damn dirty, but you can always do at the very first line:
$: << '.'
and off you go with pwd'ed require.
It's quite useful for interactive/creative testing with IRB
I believe both of the previous posts are correct, just for different uses. In IRB use an absolute path with require, with a file you can also use require with an absolute path, or use require_relative.
If you're trying to do this with rvmsudo, I found this worked for me:
rvmsudo irb -I '/Absolute/path/to/your/project'

Rails irb default directory

I'm trying to include a source code file when I run irb but irb is unable to find it.
For example, say I am in the following directory in terminal:
/dan/rubyapp/
Assume I have a file named "firstapp.rb" in /dan/rubyapp/
I startup irb and from the irb prompt I type
> require "firstapp.rb"
but the file can't be found. If I type "Dir.pwd" it shows as
/dan/rubyapp/
The only way I can get "require" to work is if I include the full path like so
> require "/dan/rubyapp/firstapp.rb"
Is that the only way I can get this to work? All the tutorials I see online simply do "require file_name" so I assumed it would work.
here is the output from $: at irb
ruby-1.9.2-p0 > $:
=> ["/Users/Daniel/.rvm/gems/ruby-1.9.2-p0/gems/wirble-0.1.3/bin",
"/Users/Daniel/.rvm/gems/ruby-1.9.2-p0/gems/wirble-0.1.3/lib",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.4.0",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/site_ruby",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.4.0",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/vendor_ruby",
"/Users/Daniel/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1",
"/Users/Daniel/.rvm/rubies/ruby-
1.9.2-p0/lib/ruby/1.9.1/x86_64-darwin10.4.0"]
The problem is that the current working directory is no longer in your path (as of Ruby 1.9.2). There are a few different ways around the problem.
1) In a ruby file itself, you can use the method require_relative instead of require. This will load a file relative to the loaction of the file containing the require_relative method:
http://extensions.rubyforge.org/rdoc/classes/Kernel.html
require_relative 'firstapp.rb'
This, however, will not work in irb.
2) Your other option is to include the current path in your argument to the require method. This will work in irb or in a ruby file. For instance:
require './firstapp.rb'
The reason this was implemented in ruby was to avoid inadvertently requiring the wrong file if there are different files with the same name in different directories in the path (similar to how *nix does not include the current directory "." in its path)
A couple of things to try:
1) Drop the .rb from the end of your require so you have:
require 'firstapp'
You don't normally add the .rb to a require (only to a load) - have a look here for more details:
http://www.fromjavatoruby.com/2008/10/require-vs-load.html
2) Failing that, make sure the current directory is on your load path - in irb execute:
p $:
and it will print out your ruby load path - check for an entry for "." (mine is the last entry)

Resources