Top Banner
Exploring metaprogramming using Ruby language A brief introduction Harshal Ganpatrao Hayatnagarkar PhD Candidate in Dept. of Computer Science 1 Friday, October 3, 2014
21

Exploring metaprogramming using Ruby language

Jul 13, 2015

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Exploring metaprogramming using Ruby language

Exploringmetaprogramming

using Ruby language

A brief introduction

Harshal Ganpatrao HayatnagarkarPhD Candidate in Dept. of Computer Science

1

Friday, October 3, 2014

Page 2: Exploring metaprogramming using Ruby language

Engineering marvels

10/24/2014 2

Ford Model T (approx. 1918) Tesla Model S (2010)

Page 3: Exploring metaprogramming using Ruby language

Assembly Lines: Process marvels

10/24/2014 3

Ford Model T Assembly Line

Tesla Robotic Assembly Line

Page 4: Exploring metaprogramming using Ruby language

Assembly Lines: Process marvels

10/24/2014 4

People building robots build robots building cars

What is this analogous to?

People building carsA worker on assembly line does same task repetitively, item after item and day after day.

People building robots building carsIs this like programming?

Courtesy: http://stackoverflow.com/questions/3468246/whats-the-use-of-metaprogramming

Page 5: Exploring metaprogramming using Ruby language

Metaphor

10/24/2014 5

People building robots building robots… building carsWhat is this analogous to?

Page 6: Exploring metaprogramming using Ruby language

Meta-programming

• Meta (‘beyond’ in Greek) + programming Programming Abstraction– Similar to meta-data, meta-model, meta-physics and so on.

• “Writing of computer programs that with ability to treat programs as their data, as it meant that a program could be designed to read, generate, analyse and/or transform other programs, and even modify itself while running. In some cases, this allows programmers to minimize the number of lines of code to express a solution (hence reducing development time), or it gives programs greater flexibility to efficiently handle new situations without recompilation.” - Wikipedia

610/24/2014

Page 7: Exploring metaprogramming using Ruby language

Revelation

10/24/2014 7

Data-Code Duality

Page 8: Exploring metaprogramming using Ruby language

Two aspects

10/24/2014 8

Reflection Generative programming

Page 9: Exploring metaprogramming using Ruby language

Reflection

• Ability of a computer program to examine and modify the structure and behavior (specifically values, meta-data, properties and functions) of the program at runtime.– Metalanguage in which metaprogram is written. – object language, programs written in which are manipulated.– reflection or reflexivity is ability of a programming language to

be its own metalanguage.

• Fairly common in programming languages– C++ (limited)– Java (+ other JVM languages), C# (+ other CLR languages)– JavaScript, Python, PHP, Ruby

10/24/2014 9

Page 10: Exploring metaprogramming using Ruby language

Generative Programming

• “A style of computer programming that uses automated source code creation through generic frames, classes, prototypes, templates, aspects, and code generators to improve programmer productivity.” - Wikipedia

• For example, – Internal to language: C/C++ preprocessor macros, C++

templates, LISP macros, Ruby/Python/PHP evaluations.

– External: Code generation templates.

10/24/2014 10

Page 11: Exploring metaprogramming using Ruby language

Ruby Programming Language

• Dynamic, reflective, object-oriented, general-purpose programming language.

• Influenced by Perl, Smalltalk, Eiffel, Ada, and Lisp.

• Supports multiple paradigms functional, object-oriented, and imperative.

• With dynamic type system and automatic memory management.

10/24/2014 11

Yukihiro "Matz" Matsumoto

Page 12: Exploring metaprogramming using Ruby language

Meta Object Protocol

• Everything is an object, including classes.

• Method calling is analogous to message passing where arguments are payload.

10/24/2014 12

Page 13: Exploring metaprogramming using Ruby language

10/24/2014 13

Page 14: Exploring metaprogramming using Ruby language

Open Classes

10/24/2014 14

Page 15: Exploring metaprogramming using Ruby language

Dynamic methods

10/24/2014 15

Page 16: Exploring metaprogramming using Ruby language

Dynamic methods

10/24/2014 16

Ruby on Rails – ActiveRecord example

Page 17: Exploring metaprogramming using Ruby language

Generative Programming

• Code generation – one or more levels– ‘eval’ expression family– Takes source code in string form and embed in current

context.

10/24/2014 17

Page 18: Exploring metaprogramming using Ruby language

Two-level generation• Code generating code generating code

10/24/2014 18

Level 1

Level 2

‘Level 1’ eval

‘Level 2’ eval

Page 19: Exploring metaprogramming using Ruby language

Pitfalls

• Metabugs?– Bugs generating bugs generating…

– And there is no ‘programs debugging programs debugging programs…’.

• Increased code complexity

10/24/2014 19

Page 20: Exploring metaprogramming using Ruby language

Further exploration…

• Model-driven development

• Domain-specific languages

• Ruby on Rails for web development framework

– An example of metaprogramming features.

10/24/2014 20

Page 21: Exploring metaprogramming using Ruby language

Questions?

And thank you for your interest !

10/24/2014 21