CSC/ECE 517 Summer 2008/wiki1 3 ref: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
Reflection is built into Ruby, but in Java, it's a special API. Does this make Ruby code easier to write than Java code? Give examples of reflection sequences in both languages, and analyze which is clearer, and also, if possible, which is more efficient. | |||
Java Reflection - | |||
The Java package for reflection is java.lang.reflect. Another important class for reflection is java.lang.Class. Java's reflection has limitations that Ruby's doesn't. An example of something Ruby can do is to iterate over all of the objects of a certain type. The ObjectSpace class in Ruby has a method each_object(), which iterates over each object that matches the type of it's parameter. | |||
a = 102.7 | |||
b = 95 # Won't be returned | |||
c = 12345678987654321 | |||
count = ObjectSpace.each_object(Numeric) {|x| p x } | |||
puts "Total count: #{count}" | |||
This prints out the value of each Numeric type object that exists in the Ruby environment, and then the count. Java's reflection api doesn't provide a mechanism to iterate over | |||
http://java.sun.com/javase/6/docs/api/java/lang/reflect/package-summary.html | |||
http://java.sun.com/javase/6/docs/api/java/lang/Class.html |
Revision as of 01:32, 7 June 2008
Reflection is built into Ruby, but in Java, it's a special API. Does this make Ruby code easier to write than Java code? Give examples of reflection sequences in both languages, and analyze which is clearer, and also, if possible, which is more efficient.
Java Reflection - The Java package for reflection is java.lang.reflect. Another important class for reflection is java.lang.Class. Java's reflection has limitations that Ruby's doesn't. An example of something Ruby can do is to iterate over all of the objects of a certain type. The ObjectSpace class in Ruby has a method each_object(), which iterates over each object that matches the type of it's parameter.
a = 102.7 b = 95 # Won't be returned c = 12345678987654321 count = ObjectSpace.each_object(Numeric) {|x| p x } puts "Total count: #{count}"
This prints out the value of each Numeric type object that exists in the Ruby environment, and then the count. Java's reflection api doesn't provide a mechanism to iterate over
http://java.sun.com/javase/6/docs/api/java/lang/reflect/package-summary.html http://java.sun.com/javase/6/docs/api/java/lang/Class.html