Hash#stringify_keys undefined - ruby

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.

Related

Migrating to rubinius

I'm trying to migrate my project from mri to rubinius to get concurrency advantage.
I've started the server and open first page and then got error:
Puma caught this error: undefined method `=~' for Pathname (NameError)
kernel/common/module.rb:212:in `instance_method'
kernel/common/module.rb:354:in `undef_method'
kernel/bootstrap/array.rb:66:in `each'
kernel/common/module.rb:352:in `undef_method'
...
My Gemfile
source 'https://rubygems.org'
ruby '2.1.0', :engine => "rbx", engine_version: '2.2.1'
gem "rubysl" # Ruby Standard Library meta-gem for rubinius
# Server requirements
gem 'puma'
...
What might be a problem here?
UPDATE: full stack trace
I examined your stack trace and looked at the Rubinius source code. The offending line is:
class Pathname
undef =~ # THIS IS IT
end
#=~ is an instance method on Object so normally undef =~ should work on any class... unless it has been undef'd on Object or on Pathname already.
I'm wondering if this is happening because you have the rubysl gem in your Gemfile. I don't know Rubinius, but from what I can see, it doesn't seem to require you to specifically include this gem. Or maybe it did in past versions, but doesn't now. If the standard library is being loaded twice, that would explain why undef =~ fails the second time.
If that doesn't help, I recommend you try temporarily removing as many gems as possible and see if the problem disappears. If so, add them back one by one until you find which one is causing the problem.

undefined method `to_a' for "include Rubeus::Swing\n":String

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.

LoadError: cannot load such file -- directory/file_name

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.

Ruby refinements not working in CI server

I'm having errors within the Jenkins server:
$ ruby -v
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
When running rspec, I have the following error:
undefined method `using' for #<Class:0x000000026f9c88> (NoMethodError)
the exact same code works on my local computer, with ruby2.
Here's my version: ruby 2.0.0dev (2012-12-01 trunk 38126) [x86_64-linux]
Also, it works on irb. It seems that ruby isn't recognizing the using statement when running a script.
Here's the code:
describe "blah" do
include TestHelper
using TestHelper::BrowserRefinement
...
end
clarification: the refinement is defined in a different file. I'm scourging the interwebs to see if there's a difference between revisions r39474 and r38126.
In current release of Ruby 2.0 (2.0.0p0), using is an instance method of the top-level object main, not that of Module. And it's a private method. If you call it in class/module definition or method definition, a RuntimeError is raised.
"The scope of a refinement activated by main.using is from the point just after main.using is invoked to the end of the file where main.using is invoked. However, when main.using is invoked in a string given as the first argument of Kernel#eval, Kernel#instance_eval, or Module#module_eval, the end of the scope is the end of the string."
You can read more about this in Refinements Specification.
For your test cases, you can write them with eval and pass in the top level bindings, like the test cases in ruby source.
Refinements is still an experimental feature, it may change in future :-)

Ruby mixin gives unidentified constant error

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.

Resources