Uploading documents to an IRB ruby

I am trying to upload a file to IRB. I have a file saved on my desktop called "Ruby.rb" How do I upload a file to IRB?

I tried using both, "download Ruby.rb", "download" Ruby "and" require "Ruby.rb". these documents are on my desktop, is there something that requires a path directory here?

The document path is C: \ Users \ Jamal \ desktop, should I include this in the load command "Ruby.rb"? THANKS

+6
source share
1 answer

It depends on the version of Ruby you are using (1.8.x or 1.9.x). load and require work as in the Ruby load path. You can look at it by evaluating $: inside IRB. In Ruby 1.9.x, the current directory is not part of the download path, so you need to use the absolute path to your file. Depending on your operating system (Windows 7), this might look like this:

 c:\Users\mliebelt\Desktop>irb irb(main):001:0> $: => ["C:/apps/ruby/ruby192/lib/ruby/site_ruby/1.9.1", "C:/apps/ruby/ruby192/lib/ruby/site_ruby/1.9.1/i386-msvcrt", "C:/apps/ruby/ruby192/lib/ruby/site_ruby", "C:/apps/ruby/ruby192/lib/ruby/vendor_ruby/1.9.1", "C:/apps/ruby/ruby192/lib/ruby/vendor_ruby/1.9.1/i386-msvcrt", "C:/apps/ruby/ruby192/lib/ruby/vendor_ruby", "C:/apps/ruby/ruby192/lib/ruby/1.9.1", "C:/apps/ruby/ruby192/lib/ruby/1.9.1/i386-mingw32"] irb(main):002:0> require 'c:/Users/mliebelt/Desktop/ruby' File c:/Users/mliebelt/Desktop/ruby.rb loaded. => true irb(main):003:0> load 'c:/Users/mliebelt/Desktop/ruby.rb' File c:/Users/mliebelt/Desktop/ruby.rb loaded. => true 

By the way, the contents of the ruby.rb file ruby.rb :

 puts "File #{__FILE__} loaded." 

The same session with IRB on Ruby 1.8.x might look like this:

 c:\Users\mliebelt\Desktop>irb irb(main):001:0> $: => ["C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby/1.8", "C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby/1.8/i386-msvcrt", "C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/site_ruby", "C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby/1.8", "C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby/1.8/i386-msvcrt", "C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/vendor_ruby", "C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/1.8", "C:/Users/mliebelt/.pik/rubies/Ruby-187-p334/lib/ruby/1.8/i386-mingw32", "."] irb(main):002:0> require 'ruby' File ./ruby.rb loaded. => true irb(main):003:0> load 'ruby.rb' File ./ruby.rb loaded. => true 

The differences between require and load are as follows:

  • require no suffix (.rb) file required
    • require 'ruby' and require 'ruby.rb' are the same
  • require reads the file into memory once, so require should be used instead of load , which reads the file in memory each time a function in the file is called.

So, to upload files (using require or load ), follow these steps: - expand your download path with the current directory (if necessary). See Adding a directory to the boot path . - (when using Ruby 1.8.x) Run your program (or IRB) in the directory from which you want to download or request files.

+9
source

Source: https://habr.com/ru/post/890904/


All Articles