Is there a better way to get the public “properties” of a Ruby object?

Is there a better way to get the public “properties” of a Ruby object?

def props
  self.public_methods.grep(/.=$/) - ["==","==="]
end
+3
source share
3 answers

Your regular expression is incomplete: it matches methods starting with any character, not just word characters. The best way to get all the "writers" is

methods.grep /^\w+=$/

And regex can be shortened to

methods.grep /\w=$/

but less clear.

+3
source

, , , - , . , .

, , height_feet= height_meters= , , , @height_feet @height_meters @height_cubits.

, , , .

+2

, Ruby , "" . , (, , , Ruby), , , , .

, "" ", , ", , . (: "" , <=, >= !=, , .)

+1

Source: https://habr.com/ru/post/1780791/


All Articles