Download presentation
Presentation is loading. Please wait.
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 objects
3
Who Am I? (said the object)
Who’s my parent? o.superclass What class am I? o.class ? 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
Interesting philosophy
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.
5
Example look up: instance_variable_set and instance_variable_get
class Cat def initialize(name, = = age end def purr puts "purrrrrr" def show_local x = 5 local_variables.each do |var| puts var def show_methods //false = only my method, not inherited Cat.instance_methods(false).each do |method| puts method.to_s 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
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??
7
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? this_obj.class.new(params)
8
Sounds pretty useful! Skim article, get as far as dump method On paper (to turn in) Describe briefly something cool you can do using reflection Then move on to next slide for another class participation question
9
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?? Class participation: on paper, draw picture of flow from database table to class to view. We’ll cover as a whole class after everyone’s given it some thought.
10
A simple example class Message_framework def message_hello puts "hello" end def message_goodbye puts "goodbye" def show_all_messages o = self # so call on this object puts "The message names“ # compare to Cat.methods.each o.methods.each do |method| if (method.to_s[0..6] == "message") puts method.to_s 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
Another use of reflection
Form generator. Given a definition of a class, generate a program that can accept user input. This is the last 2 steps of what you drew on paper.
12
In-class Challenge Write a program that uses reflection to prompt for user input Example: I have a Cat class with two attributes, name and age Program output: How do I know the class name? What data will we accept? Google – how to set
13
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 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 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)
14
On Your Own What other languages have reflection?
Is it used for different purposes depending on the language?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.