Access text file in ruby ​​jewel

I am using ruby ​​version 2.0.0, I made some custom logo in a text file called logo.txt as follows:

  _____
 |     |
 |_____|
 |
 |
 |

Now I created a gem named "custom" and placed this file under lib / logo.txt. Now I want to print this file in my script under a ruby ​​stone, so I wrote this way.

file = File.open("lib/logo.txt")
contents = file.read
puts "#{contents}"

But the above code creates errors, for example:

/usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/gems/2.0.0/gems/custom-0.0.1/lib/custom/custom.rb:1551:in `initialize': No such file or directory - lib/logo.txt (Errno::ENOENT)

I include this logo.txt file in gemspec as shown below:

Gem::Specification.new do |s| 
s.name         = "custom"
s.version      =  VERSION
s.author       = "Custom Wear"
s.email        = "custom@custom.com"
s.homepage     = "http://custom.com"
s.summary      = "custom wera"
s.description  = File.read(File.join(File.dirname(__FILE__), 'README'))
s.license      = 'ALL RIGHTS RESERVED'
s.files         = [""lib/custom.rb", "lib/custom/custom.rb", "lib/custom /version.rb","lib/logo.txt"]
s.test_files    = Dir["spec/**/*"]
s.executables   = [ 'custom' ]
s.require_paths << 'lib/'
+4
source share
1 answer

The file opens relative to the current working directory, if you did not specify the full path.

, Ruby __FILE__. custom.gemspec - :

File.join( File.dirname(__FILE__), 'README')

, :

logo_path = File.join( File.dirname(__FILE__), '../logo.txt' )
file = File.open( logo_path )

Ruby 2.0 __dir__ ( File.dirname(__FILE__)), Ruby 1.9. , , - , , - , .

+12

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


All Articles