I am following the very basic tutorial found here: http://guides.rubygems.org/make-your-own-gem/
hola_username.rb:
class Hola
def self.hi
puts "Hello world!"
end
end
hola_username.gemspec:
Gem::Specification.new do |s|
s.name = 'hola_username'
s.version = '0.0.0'
s.date = '2010-04-28'
s.summary = "Hola!"
s.description = "A simple hello world gem"
s.authors = ["Surname Lastname"]
s.email = 'me.me#gmail.com'
s.files = ["lib/hola_username.rb"]
s.homepage =
'http://rubygems.org/gems/hola_username'
s.license = 'MIT'
end
That really is all there is to the project.
I can build my gem with
gem build .\hola_username.gemspec
I have also tested it by importing and executing the hi function of the Hola class and it works:
PS E:\hola_username> gem install .\hola_username-0.0.0.gem
Successfully installed hola_username-0.0.0
Parsing documentation for hola_username-0.0.0
Done installing documentation for hola_username after 0 seconds
1 gem installed
&
irb(main):001:0> require 'hola_username'
=> true
irb(main):002:0> Hola.hi
Hello world!
=> nil
irb(main):003:0>
But when I try to
gem push .\hola_username-0.0.0.gem
I get:
ERROR: While executing gem ... (Psych::SyntaxError)
(): control characters are not allowed at line 1 column 1
Any ideas?
Edit: I am on a windows 10 machine using ruby 2.0.0p598
Edit v01: Anything I put after gem push will result in the above error, doesn't seem to be a problem with the sample rubygem.
Edit v02: My credentials file that was generated in the .gem folder however stars with hex characters: fffe2d002d00.. Which might be the ones causing trouble?
My credentials file in .gem folder was encoded with UCS2 - Little Endian and converting it to UTF without BOM did the trick.
Although I have absolutey no idea why..
Related
I am new to programming in Ruby. I was following the example of creating a new gem. But when I issue the command to build the gem (gem build packagename.spec), I am getting an error.
Here is my code snippet:
Gem::Specification.new do |s|
s.name = "moviesAndPlaylists"
s.version = "1.0.0"
s.author = "Chetan Yewale"
s.email = "yewalechetan#gmail.com"
s.summary = "Plays and reviews movies"
s.description = File.read(File.join(File.dirname(__FILE__)), 'README')
s.homepage = "http://github.com/"
s.files = Dir["{bin, lib, spec}/**/*"] + %w(LICENSE README)
s.test_files = Dir["spec/**/*"]
s.executables = [ 'moviesAndPlaylists' ]
s.required_ruby_version = '>=1.9'
s.add_development_dependecy 'rspec'
end
Please let me know what is wrong? Why do I get an error message like:
Invalid gemspec in [moviesAndPlaylists.gemspec]: no implicit conversion of String into Integer
ERROR: Error loading gemspec. Aborting.
I tried commenting out some lines and starting again, but could not figure it out. If anybody has encountered this error before, please let me know,
Just change the line
s.executables = [ 'moviesAndPlaylists' ]
to
s.executables << 'moviesAndPlaylists'
Look the documentation of executables.
I am using ruby version 2.0.0 , I made some custom logo in text file named logo.txt like this:
_____
| |
|_____|
|
|
|
Now i made a gem with name of "custom" and placed this file under lib/logo.txt . Now i wants to print this file in my script under ruby gem so i wrote in this way.
file = File.open("lib/logo.txt")
contents = file.read
puts "#{contents}"
But above code produce errors, like:
/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 per 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/'
The file is opened relative to the current working directory, unless you specify the full path.
In order to avoid hard-coding full paths, you can get the full path of the current file from Ruby using __FILE__. In fact you can see in the custom.gemspec file something very similar going on:
File.join( File.dirname(__FILE__), 'README')
I think you can get to your logo file like this:
logo_path = File.join( File.dirname(__FILE__), '../logo.txt' )
file = File.open( logo_path )
In Ruby 2.0, you also have __dir__ (which can replace File.dirname(__FILE__)) but that would not be compatible with Ruby 1.9. Generally you are safer using backward-compatible syntax in gems in case you are not sure what someone has when they run your library.
folks! I get the following error message and I have no idea what to do. Is this a already known net-ldap bug? I tried to update my gems and I already looked for further informations in the internet. The first part is ok, I get ally my data from my ldap database but this error occurs in the end.
/usr/local/lib/ruby/gems/1.9.1/gems/net-ldap-0.3.1/lib/net/ber/core_ext/string.rb:23:in
encode': "\x8E" from ASCII-8BIT to UTF-8
(Encoding::UndefinedConversionError) from
/usr/local/lib/ruby/gems/1.9.1/gems/net-ldap-0.3.1/lib/net/ber/core_ext/string.rb:23:in
raw_utf8_encoded' from
/usr/local/lib/ruby/gems/1.9.1/gems/net-ldap-0.3.1/lib/net/ber/core_ext/string.rb:15:in
to_ber' from
/usr/local/lib/ruby/gems/1.9.1/gems/net-ldap-0.3.1/lib/net/ldap.rb:1396:in
block in search' from
/usr/local/lib/ruby/gems/1.9.1/gems/net-ldap-0.3.1/lib/net/ldap.rb:1367:in
loop' from
/usr/local/lib/ruby/gems/1.9.1/gems/net-ldap-0.3.1/lib/net/ldap.rb:1367:in
search' from
/usr/local/lib/ruby/gems/1.9.1/gems/net-ldap-0.3.1/lib/net/ldap.rb:637:in
`search'
and here my code:
require 'rubygems'
require 'net/ldap'
ldap = Net::LDAP.new
ldap.host = 'xxxxxx'
ldap.authenticate "cn=admin, dc=xxxx, dc=xxxxx, dc=de", "xxxxx!"
#puts ldap.bind
if ldap.bind
# authentication succeeded
else
# authentication failed
# p ldap.get_operation_result
end
filter = Net::LDAP::Filter.eq("uid", "*")
treebase = "xxxxx, dc=xxxxxx, dc=de"
ldap.search(:base => treebase, :filter => filter) do |entry|
puts "DN: #{entry.dn}"
entry.each do |attribute, values|
puts " #{attribute}:"
values.each do |value|
puts " --->#{value}"
end
end
end
There are many encoding issues in v0.3.1 of net-ldap [1],[2],[3],[4]. Several patches are already merged, but sadly, this great project seems semi-abandoned and the changes aren't pushed out to rubygems. Using it directly from github has been working well for me, and if you're using bundler, it is as easy sticking something like this in your Gemfile:
gem "net-ldap", :git => "git://github.com/ruby-ldap/ruby-net-ldap.git", :ref => '8a182675f4'
1 - https://github.com/ruby-ldap/ruby-net-ldap/pull/41
2 - https://github.com/ruby-ldap/ruby-net-ldap/pull/44
3 - https://github.com/ruby-ldap/ruby-net-ldap/pull/64
4 - https://github.com/ruby-ldap/ruby-net-ldap/pull/55
I'm trying to build a ruby gem around a C extension. The C extension compiles fine using the ruby extconf.rb; make; sudo make install routine, but when I try to build a gem using rake, the process terminates with the error at the bottom of this trace.
I'm using the post here, with the same directory structure, to create the gem.
What's wrong with my configuration? My gemspec and Rakefile are below the trace (the gem is called netconf).
// Trace
** Execute copy:netconf:i686-linux:1.9.2
install -c tmp/i686-linux/netconf/1.9.2/netconf.so lib/netconf/netconf.so
** Execute compile:netconf:i686-linux
** Execute compile:i686-linux
** Execute compile
** Invoke chmod (first_time)
** Execute chmod
** Execute build
rake aborted!
ERROR: While executing gem ... (Gem::InvalidSpecificationException)
["extconf.rb", "netconf.o", "netconf.so"] are not files
// netconf.gemspec
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "netconf/version"
Gem::Specification.new do |s|
s.name = "netconf"
s.version = Netconf::VERSION
s.authors = ["..."]
s.email = ["..."]
s.homepage = "..."
s.summary = %q{A tool to access and write Ubuntu network configuration}
s.description = %q{Uses ifconfig and other C system calls to access network configurations on a Ubuntu install.}
s.rubyforge_project = "netconf"
s.files = `git ls-files`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.extensions = ["ext/netconf/extconf.rb"]
end
// Rakefile
require 'rake'
require 'rake/extensiontask'
require 'bundler'
Rake::ExtensionTask.new("netconf") do |extension|
extension.lib_dir = "lib/netconf"
end
task :chmod do
File.chmod(0775, 'lib/netconf/netconf.so')
end
task :build => [:clean, :compile, :chmod]
Bundler::GemHelper.install_tasks
I got this error because I hadn't commited my updates with git yet.
s.files = `git ls-files`.split("\n")
That line is directly using git, and probably causing this error. Just do
git add .
git commit -a -m "init"
FWIW, I had a similar issue where
s.files = `git ls-files`.split("\n")
was
s.files = `git ls-files`.split('\n')
, where the single quotes were preventing the file list from splitting properly. Changing to double quotes fixed the issue for me.
Is there a Ruby method I can call to get the list of installed gems?
I want to parse the output of gem list.
Is there a different way to do this?
This lists all the gems I have installed.
gem query --local
http://guides.rubygems.org/command-reference/#gem-list
Listing all installed gems
The Gem command is included with Ruby 1.9+ now, and is a standard addition to Ruby pre-1.9.
require 'rubygems'
name = /^/i
dep = Gem::Dependency.new(name, Gem::Requirement.default)
specs = Gem.source_index.search(dep)
puts specs[0..5].map{ |s| "#{s.name} #{s.version}" }
# >> Platform 0.4.0
# >> abstract 1.0.0
# >> actionmailer 3.0.5
# >> actionpack 3.0.5
# >> activemodel 3.0.5
# >> activerecord 3.0.5
Here's an updated way to get a list:
require 'rubygems'
def local_gems
Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }
end
Because local_gems relies on group_by, it returns a hash of the gems, where the key is the gem's name, and the value is an array of the gem specifications. The value is an array of the instances of that gem that is installed, sorted by the version number.
That makes it possible to do things like:
my_local_gems = local_gems()
my_local_gems['actionmailer']
# => [Gem::Specification.new do |s|
# s.authors = ["David Heinemeier Hansson"]
# s.date = Time.utc(2013, 12, 3)
# s.dependencies = [Gem::Dependency.new("actionpack",
# Gem::Requirement.new(["= 4.0.2"]),
# :runtime),
# Gem::Dependency.new("mail",
# Gem::Requirement.new(["~> 2.5.4"]),
# :runtime)]
# s.description = "Email on Rails. Compose, deliver, receive, and test emails using the familiar controller/view pattern. First-class support for multipart email and attachments."
# s.email = "david#loudthinking.com"
# s.homepage = "http://www.rubyonrails.org"
# s.licenses = ["MIT"]
# s.name = "actionmailer"
# s.require_paths = ["lib"]
# s.required_ruby_version = Gem::Requirement.new([">= 1.9.3"])
# s.requirements = ["none"]
# s.rubygems_version = "2.0.14"
# s.specification_version = 4
# s.summary = "Email composition, delivery, and receiving framework (part of Rails)."
# s.version = Gem::Version.new("4.0.2")
# end]
And:
puts my_local_gems.map{ |name, specs|
[
name,
specs.map{ |spec| spec.version.to_s }.join(',')
].join(' ')
}
# >> actionmailer 4.0.2
...
# >> arel 4.0.1,5.0.0
...
# >> ZenTest 4.9.5
# >> zucker 13.1
The last example is similar to the gem query --local command-line, only you have access to all the information for a particular gem's specification.
Both
gem query --local
and
ruby -S gem list --local
list 69 entries
While
ruby -e 'puts Gem::Specification.all_names'
return 82
I used wc -l to get the numbers. Not sure if that is the right way to check. Tried to redirect the output to text files and diffed but that didn't help - will need to compare manually one by one.
There's been a method for this for ages:
ruby -e 'puts Gem::Specification.all_names'
Gem::Specification.map {|a| a.name}
However, if your app uses Bundler it will return only list of dependent local gems. To get all installed:
def all_installed_gems
Gem::Specification.all = nil
all = Gem::Specification.map{|a| a.name}
Gem::Specification.reset
all
end
use this code (in console mode):
Gem::Specification.all_names
Here's a really nice one-liner to print all the Gems along with their version, homepage, and description:
Gem::Specification.sort{|a,b| a.name <=> b.name}.map {|a| puts "#{a.name} (#{a.version})"; puts "-" * 50; puts a.homepage; puts a.description; puts "\n\n"};nil
A more modern version would be to use something akin to the following...
require 'rubygems'
puts Gem::Specification.all().map{|g| [g.name, g.version.to_s].join('-') }
NOTE: very similar the first part of an answer by Evgeny... but due to page formatting, it's easy to miss.
Try it in the terminal:
ruby -S gem list --local
Maybe you can get the files (gems) from the gems directory?
gemsdir = "gems directory"
gems = Dir.new(gemsdir).entries
From within your debugger type $LOAD_PATH to get a list of your gems. If you don't have a debugger, install pry:
gem install pry
pry
Pry(main)> $LOAD_PATH
This will output an array of your installed gems.