Project

General

Profile

Actions

Feature #4862

closed

Struct#to_hash

Added by postmodern (Hal Brodigan) almost 13 years ago. Updated about 12 years ago.

Status:
Rejected
Target version:
-
[ruby-core:36909]

Description

Occasionally, it is necessary to convert a Struct to a Hash (especially when generating JSON from an Array of Structs). A Struct#to_hash method would be very useful in this situation.

class Struct

  #
  # Returns the Hash representation of the members and values within the struct.
  #
  def to_hash
    new_hash = {}

    each_pair do |member,value|
      new_hash[member] = value
    end

    new_hash
  end

end

Related issues 1 (0 open1 closed)

Has duplicate Ruby master - Feature #5008: Equal rights for Hash (like Array, String, Integer, Float)Rejectedmatz (Yukihiro Matsumoto)07/10/2011Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0