有没有更好的方法来获取公共的“属性"? Ruby对象? [英] Is there a better way to get the public "properties" of a Ruby object?

查看:67
本文介绍了有没有更好的方法来获取公共的“属性"? Ruby对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有更好的方法来获取Ruby对象的公共属性"?

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

def props
  self.public_methods.grep(/.=$/) - ["==","==="]
end

推荐答案

您的正则表达式不完整:它匹配以任何字符(而不只是单词字符)开头的方法.获得所有作家"的最好方法是

Your regular expression is incomplete: it matches methods that start in any character, rather than just word characters. The best way to get all the "writers" would be

methods.grep /^\w+=$/

正则表达式可以缩短为

methods.grep /\w=$/

但不清楚.

这篇关于有没有更好的方法来获取公共的“属性"? Ruby对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆