Project

General

Profile

Actions

Feature #1400

closed

Please add a method to enumerate fields in OpenStruct

Added by tpo (Tomas Pospisek) over 15 years ago. Updated about 12 years ago.

Status:
Closed
Target version:

Description

=begin
There are two ways to find out what fields an OpenStruct instance has. One is through inspect,
however that returns a String that needs to be parsed.

The second is by white box engineering, looking at OpenStructs source code and seeing that in
fact, it has a hash and getting the keys of that hash...

The second way is faster, more robust, but will break once OpenStruct will be re-engineered...

So I suggest to add an explicit method to return a list of fields in an OpenStruct instance:

--- ostruct.rb.old 2009-04-23 15:26:45.000000000 +0200
+++ ostruct.rb 2009-04-23 15:32:41.000000000 +0200
@@ -110,6 +110,15 @@
@table.delete name.to_sym
end

  • Returns an Array containing the fields of an OpenStruct instance

  • p record.fields # -> [:age, :pension, :name]

  • def fields

  • @table.keys

  • end

  • InspectKey = :inspect_key # :nodoc:

=end


Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #6056: Enhancements to OpenStructClosedmarcandre (Marc-Andre Lafortune)02/21/2012Actions
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
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0