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
最满意答案
我会把丑陋提取到辅助方法中
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 ...
-
java.lang.IllegalArgumentException:方法不能为null(java.lang.IllegalArgumentException: Method must not be null)[2022-08-12]
这可能是由一个脏的类路径引起的,其中包含不同版本的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(); 实际上,自动提交模式将在专用事务中 ...
-
使用null对象的java 8 :: method引用中的实例方法(Using instance method in java 8 :: method reference with null Object)[2022-03-29]
语言规范中的关键是讨论方法引用的运行时评估(重点介绍): 如果表单是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 ...