Ruby Require and File Locations
I am trying to use a command require
in Ruby. I am using CodeRunner to run my code. The problem I'm running into is that the require statement just uses the chunky_png line. I have a folder full of library files that this code should use. I'm not sure A) where to put the folder and B) if I need to somehow point to this folder? Here is my code, below is the error I am getting. I have a folder with library files in the same folder as the PNGdecode.rb file that I am trying to run:
require 'chunky_png'
class ChunkyPNG::Image
def at(x,y)
ChunkyPNG::Color.to_grayscale_bytes(self[x,y]).first
end
end
img = ChunkyPNG::Image.from_file('engine.png')
sobel_x = [[-1,0,1],
[-2,0,2],
[-1,0,1]]
sobel_y = [[-1,-2,-1],
[0 , 0, 0],
[1 , 2, 1]]
edge = ChunkyPNG::Image.new(img.width, img.height, ChunkyPNG::Color::TRANSPARENT)
for x in 1..img.width-2
for y in 1..img.height-2
pixel_x = (sobel_x[0][0] * img.at(x-1,y-1)) + (sobel_x[0][1] * img.at(x,y-1)) + (sobel_x[0][2] * img.at(x+1,y-1)) +
(sobel_x[1][0] * img.at(x-1,y)) + (sobel_x[1][1] * img.at(x,y)) + (sobel_x[1][2] * img.at(x+1,y)) +
(sobel_x[2][0] * img.at(x-1,y+1)) + (sobel_x[2][1] * img.at(x,y+1)) + (sobel_x[2][2] * img.at(x+1,y+1))
pixel_y = (sobel_y[0][0] * img.at(x-1,y-1)) + (sobel_y[0][1] * img.at(x,y-1)) + (sobel_y[0][2] * img.at(x+1,y-1)) +
(sobel_y[1][0] * img.at(x-1,y)) + (sobel_y[1][1] * img.at(x,y)) + (sobel_y[1][2] * img.at(x+1,y)) +
(sobel_y[2][0] * img.at(x-1,y+1)) + (sobel_y[2][1] * img.at(x,y+1)) + (sobel_y[2][2] * img.at(x+1,y+1))
val = Math.sqrt((pixel_x * pixel_x) + (pixel_y * pixel_y)).ceil
edge[x,y] = ChunkyPNG::Color.grayscale(val)
end
end
edge.save('engine_edge.png')
Mistake:
PNGdecode.rb:1:in `require': no such file to load -- chunky_png (LoadError)
source to share
Well, actually chunky_png
is a gem , that is, a package library . You have to install it via RubyGems ruby ββpackaging.
First , install RubyGems if not already installed. (If you can enter the command gem
and it will display its help, skip to the second step)
Second , install this gem using the command:
gem install chunky_png
This will download not only this library but also its dependencies, install and configure them in such a way that you can make require 'chunky_png'
from any without taking all the library files with you.
This is the usual way to install external libraries in Ruby.
Note. ... You may want to add the last command with sudo
if you are on an Ubuntu Linux system and you want to install the gem system-wide (for all users)
Note 2: if you finally want to distribute your Ruby application that requires gems, you are better off using Bundler so that other people can automatically install all your dependencies without having to parse error messages and manually install the necessary gems.
source to share