Load error - Std lib class Dir

I am required to use the class -
http://www.ruby-doc.org/stdlib-2.0/libdoc/tmpdir/rdoc/Dir.html

But I am getting error - Why so?

require "dir"

Dir.tmpdir
# =>
# ~>
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require': cannot load such file -- dir (LoadError)
# ~> from
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require'
# ~> from -:1:in `<main>'

···

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

In the documentation in the URL you posted it says that the tmpdir stuff is in the file tmpdir.rb, so you should try

#!/usr/bin/env ruby

require 'tmpdir'

puts Dir.tmpdir
__END__

Which outputs results like:

ratdog:tmp mike$ ruby try.rb
/var/folders/f3/6zk_yypd73jc88n1p6ww2f_00000gn/T

tmpdir adds a couple of methods to the core Dir class, you can see this by doing something like:

#!/usr/bin/env ruby

dir_methods = Dir.methods
puts Dir.methods.count

puts "requiring tmpdir..."
require 'tmpdir'

puts Dir.methods.count
puts Dir.methods - dir_methods
__END__

Hope this helps,

Mike

···

On 2013-06-22, at 9:37 AM, Love U Ruby <lists@ruby-forum.com> wrote:

I am required to use the class -
Class: Dir (Ruby 2.0.0)

But I am getting error - Why so?

require "dir"

Dir.tmpdir
# =>
# ~>
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require': cannot load such file -- dir (LoadError)
# ~> from
/home/kirti/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in
`require'
# ~> from -:1:in `<main>'

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Mike Stok wrote in post #1113272:

In the documentation in the URL you posted it says that the tmpdir stuff
is in the file tmpdir.rb, so you should try

#!/usr/bin/env ruby

require 'tmpdir'

Thank you very much as always!!

Why the line is needed - ` #!/usr/bin/env ruby` ? I have seen lots of
code having that line at top. I am not aware of that one.

Could you help me ?

···

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

Mike Stok wrote in post #1113272:

In the documentation in the URL you posted it says that the tmpdir stuff
is in the file tmpdir.rb, so you should try

#!/usr/bin/env ruby

require 'tmpdir'

Thank you very much as always!!

Why the line is needed - ` #!/usr/bin/env ruby` ? I have seen lots of
code having that line at top. I am not aware of that one.

Could you help me ?

The #! line (shebang line) specifies which interpreter to use. So a script starting with

#!/usr/bin/ruby

would always use the /usr/bin/ruby interpreter to run the script.

As I use chruby to let myself switch between ruby scripts the ruby I want to run for my programs is pointed at by /Users/mike/.rbenv/shims/ruby, whose directory is on my PATH. When I start a script with

#!/usr/bin/env ruby

then the env program searches my PATH for the first ruby it finds and uses that to interpret the script. This is useful to me as it lets me easily switch versions in my shell and have the ruby version I'm interested in execute the script.

It also helps when distributing a script for use on systems where you don't know if ruby is /bin/ruby, /usr/local/bin/ruby, /usr/bin/ruby, or something else - as long as the PATH is set up correctly ruby will be found.

There is a downside to /usr/bin/env in that it depends on the path of the user running it, so the same script on a system run by two different users can use different interpreters. That can cause confusion when debugging.

Hope this helps,

Mike

···

On 2013-06-22, at 12:27 PM, Love U Ruby <lists@ruby-forum.com> wrote:

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

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Mike Stok wrote in post #1113281:

The #! line (shebang line) specifies which interpreter to use. So a
script starting with

#!/usr/bin/ruby

would always use the /usr/bin/ruby interpreter to run the script.

Good explanation..

So suppose I am having Ruby 1.9.3-p374, Ruby2.0.0-p0 are installed. Now
is the line `#!/usr/bin/ruby` would also be able to the desired version
as I want ? If no then how can I run the same script with different
version of Ruby(s)?

Thanks as always!!

···

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

Using rbenv:

ratdog:tmp mike$ ls -l try.rb
-rwxr-xr-x 1 mike mike 71 22 Jun 14:02 try.rb
ratdog:tmp mike$ cat try.rb
#!/usr/bin/env ruby

puts "I'm running under #{RUBY_VERSION}"

__END__
ratdog:tmp mike$ rbenv version
2.0.0-p195 (set by /Users/mike/.rbenv/version)
ratdog:tmp mike$ ./try.rb
I'm running under 2.0.0
ratdog:tmp mike$ rbenv shell 1.9.3-p429
ratdog:tmp mike$ rbenv version
1.9.3-p429 (set by RBENV_VERSION environment variable)
ratdog:tmp mike$ ./try.rb
I'm running under 1.9.3

So the same script runs under different interpreters depending on which version I have set to execute using rbenv.

Hope this helps,

Mike

···

On 2013-06-22, at 1:19 PM, Love U Ruby <lists@ruby-forum.com> wrote:

Mike Stok wrote in post #1113281:

The #! line (shebang line) specifies which interpreter to use. So a
script starting with

#!/usr/bin/ruby

would always use the /usr/bin/ruby interpreter to run the script.

Good explanation..

So suppose I am having Ruby 1.9.3-p374, Ruby2.0.0-p0 are installed. Now
is the line `#!/usr/bin/ruby` would also be able to the desired version
as I want ? If no then how can I run the same script with different
version of Ruby(s)?

Thanks as always!!

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.