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

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
Advertisements

Computing Science Software Design and Development SOFTWARE DESIGN AND DEVELOPMENT USING PYTHON.
An introduction to pointers in c
Python Objects and Classes
Computer Programming Mr. José A. Ortiz Morris. Computer Language  Languages that the computer understands.  They are low level languages. (BINARY 1.
PHP Hypertext Preprocessor Information Systems 337 Prof. Harry Plantinga.
Input and Output ENGR 1181 MATLAB 5. Input and Output In The Real World Script files (which provide outputs given inputs) are important tools in MATLAB.
Programming in python Lesson 2.
And other languages….  Get out a piece of paper  You’ll be tracing some code (quick exercises) as we go along.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Python November 14, Unit 7. Python Hello world, in class.
JavaScript ICW: Lecture 11 Tom Chothia. Last Lecture URLs Threads, to make a process run in parallel: Make it extend Thread Give it a run method Call.
OOP! POO! Spelled backwards. Intro to OOP What is OOP?  Stands for Object Oriented Programming  Create different types of objects which can do multiple.
Intro to Programming Part of Chapter 5. Algorithms An algorithm is an ordered set of executable steps that defines a terminating process. An algorithm.
Computing Theory: BBC Basic Coding Year 11. Lesson Objective You will: Be able to define what BBC basic is Be able to annotate BBC basic code Be able.
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Getting Started Example ICS2O curriculum
| Lugano, Ruby on Rails.
Python.
Object Oriented Software Development
Hello AP Computer Science!. What are some of the things that you have used computers for?
Application Development Description and exemplification of server-side scripting language for server connection, database selection, execution of SQL queries.
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Introduction to Python
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #002 (January 17, 2015)
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Learning the skills for programming Advanced Visual Programming.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Also “open class” and other languages….  A module is a named group of methods, constants, and class variables  All classes are modules  Modules are.
Intro to Python Adriane Huber Debbie Bartlett Python Lab #1Python Lab #1 1.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
1 JavaScript
Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same.
22/11/ Selection If selection construct.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
+ Ruby and other programming Languages Ronald L. Ramos.
My Python Programmes NAME:.
Ruby Objects, Classes and Variables CS 480/680 – Comparative Languages.
RUBY by Ryan Chase.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Creating a GUI Class An example of class design using inheritance and interfaces.
31/01/ Selection If selection construct.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Very Hard Problems I am so not kidding about this. Please grab a handout.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
LO: We’re learning to outline a program using Pseudo Code.
ACCESS CHAPTER 2 Introduction to ACCESS Learning Objectives: Understand ACCESS icons. Use ACCESS objects, including tables, queries, forms, and reports.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Popup Boxes.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Getting Started With Python Brendan Routledge
Ruby Inheritance and other languages….
Ruby Reflection and other languages….
1 Python Lab #1 Intro to Python Adriane Huber Debbie Bartlett.
Use proper case (ie Caps for the beginnings of words)
ARRAYS 1 GCSE COMPUTER SCIENCE.
An Introduction to Object Orientated Programming
Spreadsheets, Modelling & Databases
Input and Output Python3 Beginner #3.
General Computer Science for Engineers CISC 106 Lecture 03
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Python 10 Mr. Husch.
Presentation transcript:

and other languages…

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

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

 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.

class Cat def initialize(name, = = 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

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

 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)

 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/what-is-reflection-and-why-is-it-useful

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?

 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

 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)

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