# Creating an object the hard way, made easy with lambda

**URL:** <https://www.ruby-forum.com/t/creating-an-object-the-hard-way-made-easy-with-lambda/85226>\
**Category:** Ruby\
**Created:** [February 11, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/creating-an-object-the-hard-way-made-easy-with-lambda/85226 "2007-02-11T18:45:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Griff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f19dbf/32.png) [@Griff](https://www.ruby-forum.com/u/Griff)\
**Post date:** [February 11, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/creating-an-object-the-hard-way-made-easy-with-lambda/85226/1 "2007-02-11T18:45:35Z")

</div>

#!/usr/bin/env ruby  
#$LastChangedRevision: 49 $

# The purpose of this sample is to demonstrate the application of

lambda

# expressions to create a rudimentary ‘person object’.

# 

# Its features Smalltalk style “message passing” and "variables are

private,

# methods are public" encapsulation.

# 

# This sample is an approximation, and drastic simplification, of many

of the

# examples you find written in Scheme, as it does not provide an OO

system for

# use by the developer.

# 

# This is OK, though, since the point of this example is only to

demonstrate

# one application of lambda expressions. You may use them to write

your own

# object system in Ruby, but then again why bother, Ruby has already

got

# what you need!

def get\_abstract\_person\_object  
first\_name = “”  
last\_name = “”  
age = -1  
my\_to\_s = lambda {  
“FIRST NAME:’#{first\_name}’, LAST NAME:’#{last\_name}’,  
AGE:’#{age}’\n”  
}  
new = lambda { |fn, ln, ag|  
first\_name = fn  
last\_name = ln  
age = ag  
nil  
}

lambda { |msg|

```
result = nil

case msg[0]
        when :new
            new.call(msg[1], msg[2], msg[3])
  when :get_first_name
    result = first_name
  when :set_first_name
    result = first_name = msg[1]
  when :get_last_name
    result = last_name
  when :set_last_name
    result = last_name = msg[1]
  when :get_age
    result = lage
  when :set_age
    result = age = msg[1]
  when :to_s
    result = my_to_s.call
end

if result != nil
  result
else
  "MESSAGE '#{msg[0]}' NOT UNDERSTOOD!"
end

```

}  
end

kristy = get\_abstract\_person\_object  
puts “Create Kristy”  
puts kristy.call([:to\_s])  
kristy.call([:set\_first\_name, “Kristy”])  
kristy.call([:set\_last\_name, “T”])  
kristy.call([:set\_age, 27])  
puts kristy.call([:to\_s])

puts “Create Steve”  
steve = get\_abstract\_person\_object  
steve.call([:set\_first\_name, “Steve”])  
steve.call([:set\_last\_name, “M”])  
steve.call([:set\_age, 48])  
puts steve.call([:to\_s])

puts  
puts “Are the two ‘objects’ really different entities? YES.\n”  
puts kristy.call([:to\_s])  
puts steve.call([:to\_s])

puts  
puts “What happens when you send a message that the object does not  
understand?”  
puts kristy.call([:foo])

puts  
puts “A “constructor” message”  
dave = get\_abstract\_person\_object  
dave.call([:new, “David”, “O”, 28])  
puts dave.call([:to\_s])
