Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7005405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:20:48+00:00 2026-05-27T21:20:48+00:00

I was hacking a to_hash to Object (I’m not saying this is a good

  • 0

I was hacking a to_hash to Object (I’m not saying this is a good idea, just an experiment). When I came across an odd issue, where IO stopped working.

#lib/object.rb
class Object
  def to_hash
    self.instance_variables.inject({}) { |hash,var| hash[var.to_s.delete("@")] = self.instance_variable_get(var); hash } 
  end
end

#run_test1.rb
require_relative 'lib/Object'
require 'FileUtils'

puts 'run test'


#run_test2.rb
require_relative 'lib/Object'

File.open('test.txt', 'w') {|f| f.write('this is a test')}

in run_test1 I get an

<internal:lib/rubygems/custom_require>:29:in `set_encoding': wrong number of arguments (0 for 1..2) (ArgumentError)
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from <internal:lib/rubygems/custom_require>:29:in `require'
        from .../run_test1.rb:2:in `<main>'

in run_test2 I get

run_test2.rb:3:in `initialize': No such file or directory - test.txt (Errno::ENOENT
        from run_test2.rb:3:in `open'
        from run_test2.rb:3:in `<main>'

(if the file exists it says the file is not open for writing)

While I’m not surprised it happened – just curious, What is going on here? Theoretically it should be fine, however what is the underlying cause.

The relevant info:

  • ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
  • XP SP3 32-bit
  • RUBYGEMS VERSION: 1.8.12
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:20:49+00:00Added an answer on May 27, 2026 at 9:20 pm

    The to_hash method is used to identify objects that coerce to Hash. In that sense, it behaves like to_ary or to_str. The method you implemented is more like to_a or to_s.

    Lots of Ruby code, including Ruby core code, checks for to_hash to figure out whether an argument is an options hash (arg.respond_to? :to_hash) and from there on follow a different execution path. This gets even more messed up by some code checking if the object is a real hash or not (Hash === arg).

    You might want to name your method to_h or something similar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'd like to prevent bots from hacking weak password-protected accounts. (e.g. this happend to
http://visualidiot.com/articles/hacking-facebook Going to any article on this website, you'll see that the main body
I'm about to start hacking my own website, hosted by my QNAP Turbo NAS
I'm hacking on a simple Cocoa app to make blocks move around the screen
I was always attracted to the world of kernel hacking and embedded systems. Has
I was hacking away the source code for plink to make it compatible with
I'm hacking around in a Firefox extension, trying to use E4X. However, I've come
I am now hacking an old C code, try to make it more C++/Boost
Web developing isn't what it used to be. It used to consist of hacking
I am hacking up a tagging application for emacs. I have got a tag

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.