I installed rubeus using the command below:
jruby -S gem install rubeus
bundle install
Then when i execute:
include Rubeus::Swing
I get this error :
undefined method `to_a' for "include Rubeus::Swing\n":String
So I think gem installed the wrong version, because method to_a is no longer supported in Ruby 1.9.x for strings
How can I fix this?
code:
require 'rubygems'
require 'java'
require 'rubeus'
include Rubeus::Swing #it's where everything crashes :-)
UPDATE
It seems the problem is with the jirb, I saved the code in a file and ran the code with jruby, and everything went well... I'm confused, what's wrong with jirb?
I'm not sure why, but I can confirm that Rubeus breaks jirb:
$ irb-jruby-1.7.3
irb(main):001:0> require 'rubeus'
=> true
irb(main):002:0> 1
NoMethodError: undefined method `to_a' for "1\n":String
Things do seem to work fine when non-interactively interpreting a script. One workaround is to simply add to_a back into String:
class String
def to_a
lines.to_a
end
end
# => nil
require 'rubeus'
# => true
include Rubeus::Swing
# => Object
The only issue I've noticed so far is that require 'rubeus' still breaks jirb in a second way: I now need to press Enter twice for every input.
Related
I have a simple gem that creates an MD5 from a string.
module SimpleMD5
require 'digest/md5'
def self.md5_string(string)
Digest::MD5.hexdigest(string)
end
end
Running bundle exec bin/console and calling the method works fine
require 'simple_md5'
SimpleMD5.md5_string('test')
=> 098f6bcd4621d373cade4e832627b4f6
Once the gem is built using rake build and rake install using the IRB console and the same example above I get an error.
NameError: uninitialized constant SimpleMD5::Digest
Am I missing a step when the gem is built?
You're missing the module definition, so use this pattern:
require 'digest/md5'
module SimpleMD5
def md5_string(string)
Digest::MD5.hexdigest(string)
end
extend self
end
The SimpleMD5 name is not generated automatically, you must declare it somewhere.
Here extend self means you can mix-in the module with include SimpleMD5 as well as just use it straight-up as you do in your example.
Don't forget two things: In Ruby return is implicit, there's no need to use that unless you're exiting your function early, and MD5 is a pretty terrible hashing algorithm for 2016. Unless you're using it for backwards compatibility, use something better like SHA256 or SHA512.
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.
I have a file structure like
root
|--lib
|--root.rb
|--extensions
|--strings.rb
I want to be able to use methods in string.rb in root.rb file.
So I added require 'extensions/strings' at the top of root.rb file.
But I get LoadError: cannot load such file -- extensions/strings.rb error.
How do I avoid this?
You can try the below:
require File.dirname(__FILE__) + "/extensions/strings"
Use require_relative if you are in Ruby 1.9 or later. In root.rb, write:
require_relative 'extensions/strings.rb'
I found the answer I am looking for here.
I used jandot's solution.
Dir[File.dirname(__FILE__) + '/extensions/*.rb'].each {|file| require file }
Edit after some testing,
For some reason, this doesn't throw any error message, but it doesn't seem to be loading the actual Ruby file.
I tried adding this to extensions/strings.rb
class Dog
def self.bark
puts "bark"
end
end
And ran it on irb.
1.9.3-p0 :001 > require 'rhapsody'
=> true
1.9.3-p0 :002 > Dog
NameError: uninitialized constant Dog
from (irb):2
from /Users/jasonkim/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
So it's not finding the Dog class in extensions/strings.rb for some reason.
Edit after reading a guid in rubygems.org
When I start irb, I had to go irb -Ilib -rextensions
The guide explains the situation this way
We need to use a strange command line flag here: -Ilib. Usually RubyGems includes the lib directory for you, so end users don’t need to worry about configuring their load paths. However, if you’re running the code outside of RubyGems, you have to configure things yourself.
require_relative '../lib/extensions/strings'
Works for me.
When I run ri Hash, the stringify_keys method shows up as an instance method in some of the listings where, each listing appears to be associated with a different gem or gem version.
But when I try calling it from the irb shell, it fails:
irb(main):035:0> h
=> {:this=>"this value", :that=>"that value"}
irb(main):031:0> h.instance_of? Hash
=> true
irb(main):032:0> h.stringify_keys
NoMethodError: undefined method `stringify_keys' for {:this=>"this value", :that=>"that value"}:Hash
from (irb):32
from /usr/local/bin/irb:12:in `<main>'
Also, when I run h.methods.sort in IRB, stringify_keys isn't listed as a method.
Can someone explain this discrepancy? Is it a matter of my environment being outdated or just something I don't understand about the Ruby documentation?
Rails (via ActiveSupport) monkey patches the stringify_keys method into Hash so you won't have it in a plain irb session. You can load the core extensions by saying:
require 'active_support/core_ext'
See the Active Support Core Extensions Guide for details on loading individual monkey patches.
In irb, I do this
class Text
include FileUtils
end
I get: NameError: uninitialized constant Test::FileUtils
If I just do: include FileUtils (i.e. now class) everthing works.
What gives?
You need to make sure Ruby knows about the FileUtils module. That module isn't loaded by default:
>> FileUtils
NameError: uninitialized constant FileUtils
from (irb):1
>> require 'fileutils'
=> true
>> FileUtils
=> FileUtils
Don't worry too much about the error NameError: uninitialized constant Text::FileUtils - when you try to include a constant that Ruby doesn't know about, it looks in a few places. In your case, first it will look for Text::FileUtils and then it will look for ::FileUtils in the root namespace. If it can't find it anywhere (which in your case it couldn't) then the error message will tell you the first place it looked.
Did you try?
class Text
include ::FileUtils
end
This assumes that FileUtils is not within a module.
This is an old thread, but still if any bumps on this thread to find an answer. One just needs to add below line on top of his code (or anywhere outside the class/method/module)
require 'fileutils'
Including in the class does not works, may be it used to work in older versions.