Ruby install jcode - ruby

I'm trying to get 'jcode' for ruby, but I type "gem install jcode" and it says nothing exists?
Does anyone know why? I'm trying to manipulate UTF-8 encoded strings.

Ruby >= 1.9 doesn't require jcode, a module to handle japanese (EUC/SJIS) strings, as it supports unicode natively.
This should fix the problem:
require 'jcode' if RUBY_VERSION < '1.9'
but must say I did not test it deeply...

Have you tried simply:
require 'jcode'
?
As it happens, jcode is part of the Ruby Standard Library.

Try not to include it at all. It should be included automatically in later versions of Ruby.

Related

get specification of executed gem from within

I have build a gem and want it to print its version.
I cant use Gem::Specification.find_by_name('mygem').version
because there are several versions of it installed.
Lets just say my program has just a single src file /bin/myruby containing this:
#!/usr/bin/env ruby
mygem_version = ???
puts "This is my gems version: #{mygem_version}"
A common convention is to create a lib/<your_gem_name>/version.rb that defines a YourGemName::VERSION constant. Then, you can refer to that constant in your gemspec, which is Ruby code that gets evaluated when the gem is built.
Read http://timelessrepo.com/making-ruby-gems for a guide that uses this approach.
If you're using Bundler (think rails), try
Bundler.definition.specs
else, make sure your gem has a VERSION constant you can ask these things
This worked for me and always returned the correct version.
#!/usr/bin/env ruby
puts "This is my gems version: #{Gem.loaded_specs['mygem'].version}"

Ruby Prime Class Issues

So I am running Ruby 1.9.3 and I am trying to use the Prime class.
I have added require mathn at the top of my .rb file
Other than the method name, this code came from the documentation page:
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/prime/rdoc/Prime.html
def prime_number(n)
Prime.each(n) do |prime|
p prime
end
end
prime_number(100)
Any ideas why this doesn't work? The error I get says
undefined method each for Prime:Class
I tried it in Ruby 1.9.3 and it worked. Tried it again in Ruby 1.8.7 and got your error message. A newer version of Ruby will probably solve your problem.
Your problem is that you did require mathn and you must have require 'mathn' at the top of your file.
Either that, or you have neglected the quotes when you described your code above.

Ruby on Windows path for requires in dir not working

I've a small ruby program that require files in the same directory. Program works perfect on my mac and when I run a test ruby script without any require it also works so. It seems the ruby program doesn't look in the current directory for the file by default. e.g. the . dir. In windows where do I need to update this so ruby does look in the current dir for requires?
Chances are that your Mac is running Ruby 1.8 and Windows is running Ruby 1.9. As of 1.9, the default load path no longer includes the current directory. A common practice is to add this to the top of your ruby file before your require statements
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'my_file.rb'
You can also use the shorthand $: instead of $LOAD_PATH:
$:.unshift File.dirname(__FILE__)
Another alternative is adding the load path on the command line instead:
ruby -I. my_ruby_file.rb
Ok, I understand now since 1.9.2 for "Security" reasons they don't allow require to work like that anymore. The neatest way I found to solve it strangely was to put './' in front of every require.
e.g.
require "./myfile.rb"
"." was removed from $: was removed from Ruby 1.9.2 to be precise. Do
puts RUBY_VERSION
puts $:.inspect
on Ruby 1.8 (what's installed on your Mac) and Ruby 1.9.2 (what's installed on your windows machine) if you don't believe me.
Why does Ruby 1.9.2 remove "." from LOAD_PATH, and what's the alternative? discusses why "." was removed.

Requiring scripts is broken when using RVM

Been running into a problem ever since I started using rvm to manage my ruby installation. Whenever I want to require another class I've written, such as require 'filename', I get a require - no such file to load error when I try to run my script. If I switch back to my system ruby using rvm use system it works again, but I'm 1.8.2 as my system ruby and some features I want to use in my code are only available in 1.9.*, which I can access through rvm. How do I fix this problem?
I'm not sure this is a problem with RVM, but a problem with Ruby 1.9 not including '.' in the current path. For instance, if you are trying to require a library at './lib/file.rb', you can't do
require "lib/file"
You have to do this:
require "./lib/file"
Have you tried that syntax for your require?
I think you want to use require_relative
http://extensions.rubyforge.org/rdoc/classes/Kernel.html

How do you check the Gem Version in Ruby at Runtime?

Is it possible to check the gem version of the currently loaded gem in a ruby/rails app?
During debugging, I would like to be able to do something like:
puts RubyGem.loaded_version(:active_support)
Anything like that exist?
puts Gem.loaded_specs["activesupport"].version
careful when comparing against Gem.loaded_specs['mini_magick'].version as it's not a String but a Gem::Version object !
the version string is accessible using Gem.loaded_specs['mini_magick'].version.version which is ugly and might not work as expected e.g. '2.2' > '2.10' !
the correct way to compare against a gem version is :
Gem.loaded_specs['mini_magick'].version < Gem::Version.create('2.0')

Resources