首页 \ 问答 \ ruby元编程 - 获取类的方法名称和参数信息(ruby metaprogramming - getting method names and parameter info for a class)

ruby元编程 - 获取类的方法名称和参数信息(ruby metaprogramming - getting method names and parameter info for a class)

我想在对象中获取类方法。 请参阅以下示例我有一个类“user.rb”

class User
  def say_name

  end

  def walk(p1)

  end

  def run(p1, p2)

  end
end

我写了下面的代码

require 'user.rb'

a = User.new

arr = a.public_methods(all = false)

上面的代码将返回方法名称,但我的问题是我想获取带参数的方法名称

def def run(p1, p2)

end

我想获取方法名称(“run”)及其参数名称(p1,p2)或参数count(2)

有人可以帮助我,提前谢谢

干杯

sameera


I want to get class methods in an object. Please see the following example I have a class "user.rb"

class User
  def say_name

  end

  def walk(p1)

  end

  def run(p1, p2)

  end
end

and I wrote the following code

require 'user.rb'

a = User.new

arr = a.public_methods(all = false)

Above code will return the method name, But my question is I want to get the method name with parameters

def def run(p1, p2)

end

I want to get the method name ("run") and its parameter names (p1, p2) or parameter count (2)

can someone help me, thanks in advance

cheers

sameera


原文:https://stackoverflow.com/questions/3889134
更新时间:2023-12-23 14:12

最满意答案

我会把丑陋提取到辅助方法中

class StatementHelper{
    static void setDouble(int index, PreparedStatement preparedStatement, Double val) throws SQLException {
        if (val == null )
            preparedStatement.setNull(index, java.sql.Types.NULL);
        else
            preparedStatement.setDouble(index, val);
    }

    //Repeat for other data types
}

然后就像那样建立你的陈述

StatementHelper.setDouble(++i, ps, balance);
StatementHelper.setInt(++i, ps, someInt);
StatementHelper.setString(++i, ps, someString);
StatementHelper.setFloat(++i, ps, someFloat);

这将使语句构建代码相对干净


I would extract the ugliness into helper methods

class StatementHelper{
    static void setDouble(int index, PreparedStatement preparedStatement, Double val) throws SQLException {
        if (val == null )
            preparedStatement.setNull(index, java.sql.Types.NULL);
        else
            preparedStatement.setDouble(index, val);
    }

    //Repeat for other data types
}

and then just build your statement like that

StatementHelper.setDouble(++i, ps, balance);
StatementHelper.setInt(++i, ps, someInt);
StatementHelper.setString(++i, ps, someString);
StatementHelper.setFloat(++i, ps, someFloat);

This would keep the statement building code relatively clean

相关问答

更多
  • 我会把丑陋提取到辅助方法中 class StatementHelper{ static void setDouble(int index, PreparedStatement preparedStatement, Double val) throws SQLException { if (val == null ) preparedStatement.setNull(index, java.sql.Types.NULL); else ...
  • 每次我想使用getView时,我都想避免编写getView!= null? 您可以使用空对象模式来避免在程序中检查!= null ,代码如下所示: (1)定义一个EmptyView类 public EmptyView { //Define a static emptyView, so that we can reuse the same object public static final EmptyView emptyView = new EmptyView(); ...
  • 您正在将文本字段值转换为某个数字 Double.parseDouble(t8.getText()); 这将抛出NumberFormatException 。 你应该在你的情况下检查textfield t8的空值可能性,以及你应该在java中的数据库中设置db NULL值。 做这样的事情。 try { value8 = Double.parseDouble(t8.getText()); } catch (NumberFormatException) { value8 = 0.0; } String ...
  • 这里,您的fullname是以下方法中的局部变量。 public void setfullName(String fullName) { fullName = (lastName + ", " + firstName); } 只需将它作为对象的属性,并使用this.fullname来指定全名。此外,将参数传递给setFullName()方法也没有意义,因为您正在做的是连接名字和姓氏。 public void setfullName() { this.fullName = (lastName ...
  • 这可能是由一个脏的类路径引起的,其中包含不同版本的Spring库。 清理它并将其与具有正确版本化依赖项的同一个Spring版本对齐。 This can be caused by a dirty classpath with different versions of Spring libraries all over the place. Cleanup it and align it to the one and same Spring version with the correct versioned ...
  • 您已经在您的Registration类中设置了值,这是您问题中的第二个类。 接下来的方法是将你的Object sd传递给你的Third Class 。 正如Stephen C所说,由于您创建了一个new SecureData()并且您没有设置它的字段值,所以它将在字面上实际为空值。 所以,你不需要创建SecureData() ,只需在第三个类中传递你的sd对象。 像你的第三类一样 : public void someMethod(SecureData sd) { String UserNameLog = ...
  • 尝试切换事务模式而不是自动提交模式,如下所示: conn = DriverManager.getConnection("jdbc:sqlite:***"); // Set auto-commit to false which enable the transactional mode conn.setAutoCommit(false); ... // Explicitly commit statements to apply changes conn.commit(); 实际上,自动提交模式将在专用事务中 ...
  • 语言规范中的关键是讨论方法引用的运行时评估(重点介绍): 如果表单是ExpressionName :: [TypeArguments]标识符或主:: [类型参数]标识符... .... 目标引用是在评估方法引用表达式时确定的ExpressionName或Primary的值 。 因此,如果您以后更改目标引用的值,则无关紧要。 The key line in the language spec is where it is discussing the runtime evaluation of method ...
  • 解决此问题的一种方法是使用另一个用于NullPointerException的catch语句,然后调用在Date为null时调用的代码。 您还必须将变量“File f”移出try-statement块以确保在catch中访问。 例, catch(NullPointerException e) { System.out.println("File: " + f.getAbsolutePath() + "\tDATETIME_ORIGINAL: no data!"); } One solution t ...
  • 错误消息告诉您问题: Class<*>是非null类型,因此强制转换为始终检查转换值是否为null。 您可以将null as Class<*>?写null as Class<*>? ,但这相当于(Class) null 。 你想要null as Array>? 代替。 然而,无论如何,这似乎毫无意义: “如果parameterTypes为null,则将其视为空数组”,因此.getMethod("removeBondNative")应该给出相同的结果。 The error message t ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。