requiring a custom gem - ruby

I forked https://github.com/evolve75/RubyTree and made a few changes for usage in an application. I also added a rubytree.gemspec (which was not present in the original repo) in order to install it with bundler using a Gemfile in my application:
gem 'rubytree', :git => 'git#github.com:anthonylebrun/RubyTree.git'
I do a bundle install in my app and everything goes smoothly but I'm not able to
require 'rubygems'
require 'tree'
and just have it work. Instead I get:
LoadError: no such file to load -- tree
So I suspect I may be setting up paths wrong? I'm no gemspec pro, I just threw it together by looking at another gem. Here it is:
Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'rubytree'
s.version = '0.8.1'
s.summary = 'Tree data structure'
s.description = 'RubyTree allows you to create and manipulate tree structures in ruby'
s.required_ruby_version = '>= 1.8.7'
s.required_rubygems_version = ">= 1.3.6"
s.author = 'Anupam Sengupta'
s.email = 'anupamsg#gmail.com'
s.homepage = 'http://rubytree.rubyforge.org/'
s.files = ['lib/tree.rb']
s.require_path = ['lib']
end
Any ideas? If there's anything I can clarify, let me know too!

Have you tried this?
require 'rubygems'
require 'bundler/setup'
require 'tree'

Have you tried
require 'rubytree'
as well?

Related

Not sure why my rake seed file isn't running

here is my seed file :
require 'pry'
require 'rest-client'
require 'json'
require 'faker'
Consumer.delete_all
AlcoholicBeverage.delete_all
Intake.delete_all
100.times do
name = Faker::Name.first_name
sex= Faker::Gender.binary_type
weight= Faker::Number.between(from: 1, to: 10)
Consumer.create!(name:name,sex:sex,weight:weight)
end
ingredients=RestClient.get("https://raw.githubusercontent.com/teijo/iba-cocktails/master/recipes.json")
#ingredients_data=JSON.parse(ingredients)
#ingredients_data.collect do |x,y|
AlcoholicBeverage.create(cocktail_name: x["name"],glass: x["glass"],garnish: x["garnish"],preparation: x["preparation"])
end
100.times do
consumer_id = rand(1..100)
alcoholic_beverage_id = rand(1..100)
Intake.create!(consumer_id: consumer_id, alcoholic_beverage_id:alcoholic_beverage_id)
end
here is my gemfile:
# frozen_string_literal: true
source "https://rubygems.org"
gem "activerecord", '~> 5.2'
gem "sinatra-activerecord"
gem "sqlite3", '~> 1.3.6'
gem "pry"
gem "require_all"
gem "faker"
gem 'rest-client'
I've already ran my migrations fine.. so I'm not sure why nothing is showing up when I enter rake db:seed into my terminal.
Any advice or help will be much appreciated. I've also tried including require 'faker' in my seed file as well but it didn't change a thing.
This alternative approach will help you avoid missing data, by not depending on your ids being from 1 to 100:
consumers = 100.times.map do
name = Faker::Name.first_name
sex= Faker::Gender.binary_type
weight= Faker::Number.between(from: 1, to: 10)
Consumer.create!(name:name,sex:sex,weight:weight)
end
ingredients=RestClient.get("https://raw.githubusercontent.com/teijo/iba-cocktails/master/recipes.json")
#ingredients_data=JSON.parse(ingredients)
beverages = #ingredients_data.map do |x,y|
AlcoholicBeverage.create(cocktail_name: x["name"],glass: x["glass"],garnish: x["garnish"],preparation: x["preparation"])
end
100.times do
Intake.create!(consumer: consumers.shuffle.first, alcoholic_beverage: beverages.shuffle.first)
end

'rake native' fails with "undefined method split for nil:NilClass"

I'm creating a Ruby C extension for which I would like to create prepackaged binary packages, as the compilation requires a fair amount of dependencies. I'm new to Ruby, but it seems like gem doesn't want to create platform-specific packages, so the common way is to use the rake-compiler gem, and do rake native. I can get it to compile the extension, but then it fails with the error
NoMethodError: undefined method `split' for nil:NilClass
Running with --trace shows that the error is in rake-compiler-1.0.7/lib/rake/extensiontask.rb:515:
def ruby_api_version(ruby_version)
ruby_version.split(".")[0, 2].join(".")
end
This is because ruby_version is nil. It is called from rake-compiler-1.0.7/lib/rake/extensiontask.rb:262 in define_native_tasks():
ruby_versions = #ruby_versions_per_platform[platf] || []
sorted_ruby_versions = ruby_versions.sort_by do |ruby_version|
ruby_version.split(".").collect(&:to_i)
end
spec.required_ruby_version = [
">= #{ruby_api_version(sorted_ruby_versions.first)}",
"< #{ruby_api_version(sorted_ruby_versions.last).succ}.dev"
]
#ruby_versions_per_platform is set to {} in the constructor, but no line of code ever sets it. Thus, sorted_ruby_versions is empty, and .first and .last are nil, which results in the argument to ruby_api_version being nil.
Looking at git blame in the project shows that the these lines are 2 years old, and a result of the changeset https://github.com/rake-compiler/rake-compiler/commit/0dc23504cb03ed2fb3c506e1bb58af48d3851d1e. However, ruby_versions_per_platform is never assigned to outside the constructor, which means ruby_api_version will always get called with nil and will always fail. So how can this have worked for two years?
And more importantly, what do I have to do to get rake native to actually work?
I am using macOS 10.12.6, Ruby 2.3.7p456 installed with MacPorts (selected with port select --set ruby ruby26), rake-compiler 1.0.7, and rake-12.3.2.
Rakefile:
require "rake/extensiontask"
gemspec = Gem::Specification::load("mygem.gemspec")
Gem::PackageTask.new(gemspec) do |pkg|
end
Rake::ExtensionTask.new("mygem", gemspec) do |ext|
end
mygem.gemspec
Gem::Specification.new do |s|
s.name = "mygem"
s.version = "1.0.0rc1"
s.authors = [ "John Doe" ]
s.summary = "Awesome library"
s.description = "Awesome library because, reasons"
s.licenses = [ 'MIT' ]
s.email = "johndoe#null.com"
s.homepage = "http://www.null.com"
s.extensions = %w[ext/mygem/extconf.rb]
s.files = FileList['lib/**/*.rb', 'ext/**/*.{rb,c,h}']
s.platform = Gem::Platform::RUBY
s.required_ruby_version = '>= 1.8'
end
My project layout is
Rakefile
mygem.gemspec
ext/
mygem/
extconf.rb
mygem.c (generated with SWIG 3.0)
lib/
mygem.rb

Require in gem don't work

I created gem with bundler and puts all my ruby files into '/lib' as documentation suggested.
But I have a problem, after build the gem which "rake build" command and install (gem install pkg/gem) I can't use it because:
LoadError: cannot load such file -- mygem/client
this is cause because in main file i try to require 'mygem/client.rb' which is in lib/mygem/client.rb
and it is doesn't work :/
This is my gemspec:
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'diggy/version'
Gem::Specification.new do |spec|
spec.name = "diggy"
spec.version = Diggy::VERSION
spec.authors = [""]
spec.email = [""]
spec.summary = %q{: Write a short summary, because Rubygems requires one.}
spec.description = %q{: Write a longer description or delete this line.}
spec.homepage = ""
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
else
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
end
spec.files = `git ls-files -z`.split("\x0")
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0"
end
Assuming your main file is named mygem.rb and is inside the lib folder, you should be able to require the file lib/mygem/client.rb with:
require 'mygem/client'
Notice that I didn't use the .rb extension.

Rubygem creation ... I have pushed a new gem but the Source Code link is missing

I created and pushed a new gem for the first time in several years. I am a newbie in the Gem business.
I followed the various instructions and the gem was successfully pushed. Here is the link to new gem:
https://rubygems.org/gems/yequel
The only problem is that the 'Source Code' link does not appear on the above page. I have attached the gemspec that was used to push the gem.
Please help me identify what I am missing.
Thanks ... Al
#----------------------------------------------------------------------------
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'yequel/version'
Gem::Specification.new do |spec|
spec.name = "yequel"
spec.version = Yequel::VERSION
spec.authors = ["Al Kivi"]
spec.email = ["al.kivi#vizi.ca"]
spec.summary = %q{Provides a sequel style ORM layer for YAML::Store}
spec.description = %q{Yequel provides a sequel style with basic features to access YAML::Store tables. Its target audience is application developers who require light weight alternative to SQL databases.}
spec.homepage = "https://rubygems.org/profiles/vizi_master"
spec.license = "MIT"
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.
#if spec.respond_to?(:metadata)
#spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
#else
#raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
#end
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.11"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_runtime_dependency "hash_dot"
spec.add_runtime_dependency "will_paginate"
end
The Source link isn’t specified in the gemspec, only the homepage (which could point to a code repository).
If you want to set the the source link you need to do it on the Rubygems site. Sign into rubygems.org, and go to https://rubygems.org/gems/yequel/edit (there should be a link to this page from your gem’s page in the “Links” section, if you are signed in). From there you should be able to set the Source Code URL, along with a range of other URLs.

Problems with require in gem

I am trying to make a simple gem which has some modules. However, after I have built and installed the gem and try require it in a script I get an error:
from ...ruby-1.9.3-p429/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- my_gem/some_class (LoadError)
And
from ...ruby-1.9.3-p429/gems/my_gem-0.0.1/lib/my_gem.rb:2:in `<top (required)>'
Line 2 in my_gem.rb is require "my_gem/some_class.
As far as I understand (from this question) the problem seems to be that the file I require isn't found.
What is going on? Why can't I require my own files inside the gem?
The files I want to require is "next to" the version.rb.
#File Structure
#
# root
# |->lib
# | |->my_gem.rb
# | |->my_gem
# | | |->some_class.rb
# | | |->some_class2.rb
# | | |->version.rb
The SomeClass looks like this:
module MyGem
class SomeClass
def self.someMethod
...
end
end
All I do in the script where I get the error is the following:
#!/usr/bin/ruby
require "rubygems"
require "my_gem"
Gemspec:
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'my_gem/version'
Gem::Specification.new do |spec|
spec.name = "my_gem"
spec.version = MyGem::VERSION
spec.authors = ["My Name"]
spec.email = ["my_email#gmail.com"]
spec.description = %q{A simple Ruby gem that fails.}
spec.summary = %q{Failing gem for Ruby.}
spec.homepage = "http://some.website.com"
spec.license = "MIT"
spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
end
It looks like you have not added your class files to git, while your Gemspec file uses common pattern to specify files to be added to gem:
spec.files = `git ls-files`.split($/)
You have two options not to run into same trouble again:
Add all the files to version control before building a gem;
Use hand-written spec.files list in Gemspec.
Or, if you are using svn, you might want to change the spec.files to:
spec.files = `svn list`.split($/)
Hope it helps.

Resources