[#23231] What do you think about changing the return value of Kernel#require and Kernel#load to the source encoding of the required file? — =?ISO-8859-15?Q?Wolfgang_N=E1dasi-Donner?= <ed.odanow@...>

Dear Ruby developers and users!

8 messages 2009/04/17

[#23318] [Feature #1408] 0.1.to_r not equal to (1/10) — Heesob Park <redmine@...>

Feature #1408: 0.1.to_r not equal to (1/10)

19 messages 2009/04/26

[ruby-core:23292] [Feature #1400] Please add a method to enumerate fields in OpenStruct

From: Tomas Pospisek <redmine@...>
Date: 2009-04-23 13:34:14 UTC
List: ruby-core #23292
Feature #1400: Please add a method to enumerate fields in OpenStruct
http://redmine.ruby-lang.org/issues/show/1400

Author: Tomas Pospisek
Status: Open, Priority: Normal

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:
 
   #


----------------------------------------
http://redmine.ruby-lang.org

In This Thread

Prev Next