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

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

Related

What object processes new Gem::Specification objects?

What object/process picks up a newly created Specification object when running gem build against a typical gemspec file?
For instance, suppose we have mynewgem.gemspec with contents as follows:
Gem::Specification.new do |s|
s.name = "mynewgem"
s.version = "0.0.1"
s.summary = "here is a summary"
s.description = "here is a desc"
s.authors = ["Full Name"]
s.email = "myname#mydomain.net"
s.files = Dir.glob("lib/**/*", File::FNM_DOTMATCH)
s.homepage = "https://rubygems.org/gems/mynewgem"
s.license = "My License"
s.add_dependency "this-dependency", "~> 1.2.3"
s.add_dependency "that-dependency", "~> 4.5.6"
end
What class/method is referencing the newly created Gem::Specification object here? It's obviously not directly assigned to a variable in mynewgem.gemspec. How does whatever sees this new object actually see/reference it?
What object/process picks up [the] newly created Specification object when running gem build against a typical gemspec file?
Please see Gem::Commands::BuildCommand#build_package
# rubygems/commands/build_command.rb
def build_package(gemspec)
spec = Gem::Specification.load(gemspec)
if spec
Gem::Package.build(
# etc ...

Not able access data in "it" rails rspec test class

When we are trying to execute the below code we are not able access data newAccount and also not able to search date in active records in it method
Code Extract:
describe "search with name" do
before :each do
newAccount = Account.new
newAccount.firstname = "涯噘暮"
newAccount.id = "405"
newAccount.save
data = Account.execute_sql(['Select * from account where sfid= ? ','1'])
puts "Debug : #{data.to_json}"
newAccount = Account.new
newAccount.firstname = "涯 噘 暮"
newAccount.id = "2"
newAccount.save
end
it "should return direct matches during search" do
puts "#{newAccount}"
puts "#{Account.find_by_id(405)}"
end
end
Details of gem lib: These are the libraries that we are using for testing.
gem 'rspec-rails', '2.5.0'
gem 'faker'
gem 'capybara'
gem 'guard-rspec'
gem 'launchy'
gem 'simplecov', :require => false
gem 'resque_spec'
You have 2 errors here:
Variable names in Ruby are case-sensitive, so newaccount is different from newAccount, use one of them, preferably Ruby style guide recommends snake case (new_account)
newAccount in before(:each) is not visible in any it block. Consider them as different methods of one object, this way each method has its own local variables, that are invisible for other variables. The solution is to use instance variables, that can be shared inside different methods of one object.
This code should work:
describe "search with name" do
before :each do
#newAccount = Account.new
#newAccount.firstname = "涯噘暮"
#newAccount.id = "405"
#newAccount.save
data = Account.execute_sql(['Select * from account where sfid= ? ','1'])
puts "Debug : #{data.to_json}"
#newAccount = Account.new
#newAccount.firstname = "涯 噘 暮"
#newAccount.id = "2"
#newAccount.save
end
it "should return direct matches during search" do
puts "#{#newAccount}"
puts "#{Account.find_by_id(405)}"
end
end
You need to check if your account is really saved. You can
Add p #newAccount.errors.full_messages after #newAccount.save to see if there are any validation errors preventing your model to be saved.
Use #newAccount.save! instead of #newAccount.save (note the ! character). This will throw an exception if your save was unsuccessful.

Ruby Error: koala no such file to load LoadError

I have installed Ruby (not Rails) on a machine and am trying to run some code based off the koala framework for Facebook.
When I run
gem list
koala is mentioned but when I run the file, I get this error. Rubygems is already installed, I'm not sure what else to do. Any ideas?
Edit:
require 'koala'
require 'json'
#graph = Koala::Facebook::API.new("CAACEdEose0cBANb7YuygrBflSkBrpdalb4e70T5lJgdLPYEh0Uxy5JLPVdukKSiwZAK8g27DnwscSUWNaC0s53ogq6h562LETjYO4sB5lZAMAy8tC0SM9UzqXkk7GKYpaLrkQlgj1oLTdOJhBfq5KJtFxZBkOkpz8HaVPLYp66OnuGkaGOogVseR1tUNXVxToKl6ZCmwHL0i5RHNvMnd")
url = File.open("urls.txt","r")
url.each_line do |line|
id = /[\d]+/.match(line)
begin
temp = #graph.get_object(id)
list = File.open("working.txt", "a")
list.write(id)
list.write("\n")
puts "Worked for #{id}."
rescue
puts "Didn't work for #{id}."
end
end

ruby requiring an arbitrary ruby gem in irb session

I've been playing around with Rails for some time. But now I am attempting to build a ruby gem. And I am using rubymine which builds a gem template for you. In my case it looks like this:
$ ls
bin Gemfile lib Rakefile test
binarytree.gemspec Gemfile.lock LICENSE.txt README.md
merlino#johnmerlino:~/Documents/github/binarytree$
Inside the lib directory, I have a file called binarytree.rb, which contains the following contents:
require "binarytree/version"
module Binarytree
class BinaryNode
attr_accessor :value, :left, :right
def initialize(value=nil)
#value = value
#left = nil
#right = nil
end
def add(value)
if value <= #value
if #left
#left.add value
else
#left = BinaryNode.new value
end
else
if #right
#right.add value
else
#right = BinaryNode.new value
end
end
end
end
class BinaryTree
attr_accessor :root
def initialize
#root = nil
end
def add(value)
if !#root
#root = BinaryNode.new value
else
#root.add value
end
end
def contains(value)
node = #root
while node
if value == node.value
return true
elsif value < node.value
node = node.left
else
node = node.right
end
end
false
end
end
end
What I want to be able to do is run an irb (interactive ruby shell) session, and then be able to require 'binarytree' and have this code inside scope of irb, so I could start playing with it e.g. BinaryTree.new.
Right now I am not sure how to require this in irb:
require 'binarytree'
LoadError: cannot load such file -- binarytree
from /home/merlino/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in require'
from /home/merlino/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:inrequire'
from (irb):1
from /home/merlino/.rvm/rubies/ruby-2.0.0-p0/bin/irb:13:in `'
I am on Ubuntu and I am using rvm to manage gems.
Any ideas?
You have two options:
Go into catalog of your gem and run require './lib/binarytree.rb'
Run rake install inside catalog of your gem - this will build and install this gem into system gems.
I got it working the following way:
1) You first need to edit the gemspec:
binarytree.gemspec
And edit the description and summary lines as so:
spec.description = "binary tree"
spec.summary = "binary tree summary"
Otherwise you will get the following error:
gem build doctor_toons.gemspec
ERROR: While executing gem ... (Gem::InvalidSpecificationException)
"FIXME" or "TODO" is not a description
2) Then run the gemspec as so:
gem build binarytree.gemspec
This should output something that looks like this:
binarytree-0.0.1.gem
3) Now if you are using rvm, make sure you are using the version you want, and run the following:
gem install ./binarytree-0.0.1.gem
The output should look something like this:
Successfully installed binarytree-0.0.1
Parsing documentation for binarytree-0.0.1
Installing ri documentation for binarytree-0.0.1
Done installing documentation for binarytree after 0 seconds
Done installing documentation for binarytree (0 sec).
1 gem installed
4) Then launch irb and require the new gem:
irb(main):001:0> require 'binarytree'

requiring a custom gem

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?

Resources