1 / 12

Ruby Reflection

Ruby Reflection. and other languages… . Reflection. Also called introspection Program can examine/modify its own state set variables call methods add new methods define new classes. Who Am I? (said the object). Who’s my parent? o.superclass. What class am I? o.class. ?. Am I an X?

ping
Download Presentation

Ruby Reflection

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ruby Reflection and other languages…

  2. Reflection • Also called introspection • Program can examine/modify its own state • set variables • call methods • add new methods • define new classes

  3. Who Am I? (said the object) Who’s my parent? o.superclass What class am I? o.class ? Am I an X? o.is_a? o.kind_of? Can I do this? o.respond_to? What are my variables? o.local_variables (private) o.global_variables (private) o.instance_variables What else can I do? o.methods

  4. Example class Cat def initialize(name, age) @name = name @age = age end def purr puts "purrrrrr" end def show_local x = 5 local_variables.each do |var| puts var end end def show_methods Cat.instance_methods(false).each do |method| puts method.to_s end end end cat = Cat.new("Fluffy", 6) puts cat.class puts "Cat? #{cat.is_a? Cat}" puts "String? #{cat.is_a? String}" puts "Kind of cat? #{cat.kind_of? Cat}" puts "Kind of object? #{cat.kind_of? Object}" puts "Instance variables" cat.instance_variables.each do |var| puts var end puts "Local variables" cat.show_local puts "Methods" cat.show_methods look up: instance_variable_set and instance_variable_get

  5. Why might we do this? Think about the unit tests in Ruby. You write a method with the name test_something. It gets run automatically. How does that happen??

  6. OK, why else? Think about a game program. The user has a game piece that’s become really powerful. They now roll the die and are able to “spawn” a new object of that type. How can we create a new instance of that class?

  7. Cool, anything else? Anyone heard of Rails? ActiveRecord? Object-relational mappers… associate the rows of a database table with objects, and the columns of a database table with instance variables. Lot of cool “magic” in frameworks! Including the ability to dynamically generate “views” to accept data. How do they do that??

  8. Sounds pretty useful! http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful

  9. A simple example class Message_framework def message_hello puts "hello" end def message_goodbye puts "goodbye" end def show_all_messages o = self puts "The message names" o.methods.each do |method| if (method.to_s[0..6] == "message") puts method.to_s end end end def run_all_messages # now execute the methods puts "\nThe messages" self.methods.each do |method| if (method.to_s[0..6] == "message") self.send method end end end end messages = Message_framework.new messages.show_all_messages messages.run_all_messages How does this compare to a unit test framework?

  10. Quick Google Turn this in for class participation What other languages have reflection? Is it used for different purposes depending on the language?

  11. In-class Challenge Write a program that uses reflection to prompt for user input (like a form generator, but no real GUI) Example: I have a Cat class with two attributes, name and age Program output:

  12. More challenge details Put your class in one file (e.g., my Cat is in cat.rb) The only methods you need are initialize (so that you set up the instance variables) and to_s (for convenient display) Create a class and object named Input_framework Pass an object of your type (e.g., Cat) to your Input_framework object. Using reflection, prompt the user to enter the data. Using reflection, store the data entered in the object To verify, print the contents of the object

More Related