Presentation is loading. Please wait.

Presentation is loading. Please wait.

And other languages….  Also called introspection  Program can examine/modify its own state set variables call methods add new methods define new classes.

Similar presentations


Presentation on theme: "And other languages….  Also called introspection  Program can examine/modify its own state set variables call methods add new methods define new classes."— Presentation transcript:

1 and other languages…

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

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

4  As to why both is_a? and kind_of? exist: I suppose it's part of Ruby's design philosophy. Python would say there should only be one way to do something; Ruby often has synonymous methods so you can use the one that sounds better. It's a matter of preference. It may partly be due to Japanese influence: I'm told that they will use a different word for the same number depending on the sentence in order to make it sound nicer. Matz may have carried that idea into his language design. http://stackoverflow.com/questions/3893278/ruby-kind-of-vs-instance-of-vs-is-a

5 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 def show_methods //false = only my method, not inherited Cat.instance_methods(false).each do |method| puts method.to_s 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

6  Think about the unit tests in Ruby. You write a method with the name test_something. It gets run automatically.  How does that happen??

7  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? this_obj.class.new(params)

8  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??

9  http://stackoverflow.com/questions/3762 8/what-is-reflection-and-why-is-it-useful

10 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 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 messages = Message_framework.new messages.show_all_messages messages.run_all_messages How does this compare to a unit test framework?

11  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: What data will we accept? How do I know the class name? Google – how to set

12  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 named Input_framework Initialize an object of type Input_framework with an object of your type (e.g., I would pass in a Cat object) Get started by printing the instance variable names (e.g., I display @name and @age) 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 (uses the to_s method of your class)

13  What other languages have reflection?  Is it used for different purposes depending on the language?


Download ppt "And other languages….  Also called introspection  Program can examine/modify its own state set variables call methods add new methods define new classes."

Similar presentations


Ads by Google