Working with Objects

hedgebornabaloneΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 3 χρόνια και 7 μήνες)

70 εμφανίσεις

1

Venkat Subramaniam


svenkat@cs.uh.edu

Working with Objects

2

Venkat Subramaniam


svenkat@cs.uh.edu

Introspection


ObjectSpace.each_object(
type
) allows
you to iterate over all objects of that type


does not fetch objects of Fixnum, true, false,
and nil



methods tells us all methods an object
will respond to


remember you can add methods to an object
any time

3

Venkat Subramaniam


svenkat@cs.uh.edu

Introspection…

4

Venkat Subramaniam


svenkat@cs.uh.edu

Ways to Call a Method

5

Venkat Subramaniam


svenkat@cs.uh.edu

Type Related Methods


class


tells you the class of object


kind_of?


true if object is of specified kind or of its
derived kind


instance_of?


true if object is of specified kind


ancestors


class of object, superclasses, and all
modules

6

Venkat Subramaniam


svenkat@cs.uh.edu

Event Callbacks


You can be notified when


Methods are added or removed


Classes are subclassed


Modules are mixedin





7

Venkat Subramaniam


svenkat@cs.uh.edu

Serialization & YAML


Serialization is common features in
languages like Java, C#


Useful for storing object state and also in
distrubuted computing



Binary Serialization


Marshal in Ruby


You may also use XML


YAML comes in handy


Uses name value pairs

8

Venkat Subramaniam


svenkat@cs.uh.edu

Creating YAML

9

Venkat Subramaniam


svenkat@cs.uh.edu

Reading YAML