From Functional Style to OO

I'm trying to implement some short functional-style script in an OO
program, and I'm having problems printing the results of the OO version.

Here is the short script:

p = Proc.new { |text| text }
puts p.call( 'Some text gets printed')

=> Some text gets printed

Now the OO version of the above (a bit more involved, but this is just an
exercise for my comprehension):

class Class1
  def initialize
    @instance = self
    @class2 = Class2.new(@instance)
  end
  def print_text(text)
    puts text
  end
end

class Class2
  def initialize(instance)
    @instance = instance
    @instance.print_text('Some text gets printed')
  end
end

# Run program
puts print_it = Class1.new

=begin
Result:
Some text gets printed
#<Class1:0x000001030aebb0>
=end

I don't seem to be able to get rid of the #<Class1:0x000001030aebb0> line.
I've tried to use to_s in several places to no avail (as shown in Dave
Thomas' book).

What am I doing wrong?

Thanks in advance.

It should be clearer what's going on if you replace this line:

  puts print_it = Class1.new

with this:

  print_it = Class1.new
  puts print_it

Just calling Class1.new is what triggers the "Some text gets printed" line
- Class1.new calls Class2.new and Class2.new calls print. But the *return
value* of Class1.new is an instance of the class, which gets assigned to
the variable print_it. When you try to print that instance, there is no
to_s defined for it so it just prints the generic ruby representation of an
object.

Try this too:

class Class1
  def initialize
    @instance = self
    @class2 = Class2.new(@instance)
  end
  def print_text(text)
    puts text
  end

  def to_s
    "This is an instance of class 1"
  end
end

class Class2
  def initialize(instance)
    @instance = instance
    @instance.print_text('Some text gets printed')
  end
end

print_it = Class1.new
puts print_it

···

On Tue, Sep 13, 2016 at 11:52 AM, Marc Chanliau <marc.chanliau@gmail.com> wrote:

I'm trying to implement some short functional-style script in an OO
program, and I'm having problems printing the results of the OO version.

Here is the short script:

p = Proc.new { |text| text }
puts p.call( 'Some text gets printed')

=> Some text gets printed

Now the OO version of the above (a bit more involved, but this is just an
exercise for my comprehension):

class Class1
  def initialize
    @instance = self
    @class2 = Class2.new(@instance)
  end
  def print_text(text)
    puts text
  end
end

class Class2
  def initialize(instance)
    @instance = instance
    @instance.print_text('Some text gets printed')
  end
end

# Run program
puts print_it = Class1.new

=begin
Result:
Some text gets printed
#<Class1:0x000001030aebb0>
=end

I don't seem to be able to get rid of the #<Class1:0x000001030aebb0> line.
I've tried to use to_s in several places to no avail (as shown in Dave
Thomas' book).

What am I doing wrong?

Thanks in advance.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

That's probably being printed by the "puts" in "puts print_it =
Class1.new". Remove that puts, and that line should go away.

-Dave

···

On Tue, Sep 13, 2016 at 2:52 PM, Marc Chanliau <marc.chanliau@gmail.com> wrote:

I don't seem to be able to get rid of the #<Class1:0x000001030aebb0> line.

--
Dave Aronson, consulting software developer of Codosaur.us,
PullRequestRoulette.com, Blog.Codosaur.us, and Dare2XL.com.

The reason you get the "Class1" line is because your last line is a puts
and the argument is the expression "print_it = Class1.new" and an
assignment will resolve to the value being assigned; in this case your
instance of Class1. You're already explicitly printing your text in in the
print_text method in your first class, so really what you want is probably
just "Class1.new" -

That said it's awkward to have two classes to just print something and to
assign self to an instance variable like that, or do it as a side effect of
instantiating the object. Really you probably want to print something when
ask it to print explicitly. A more traditional OO approach would be to have
one class that takes a constructor argument, and then call a method on an
instance IE

class KlassyPrinter
  def initialize(text)
    @text = text
  end
  def print_text
    puts @text
  end
end

muhprinter = KlassyPrinter.new('Howdy')
muhprinter.print_text
=> "Howdy"

Cheers

···

On Tue, Sep 13, 2016 at 2:52 PM, Marc Chanliau <marc.chanliau@gmail.com> wrote:

I'm trying to implement some short functional-style script in an OO
program, and I'm having problems printing the results of the OO version.

Here is the short script:

p = Proc.new { |text| text }
puts p.call( 'Some text gets printed')

=> Some text gets printed

Now the OO version of the above (a bit more involved, but this is just an
exercise for my comprehension):

class Class1
  def initialize
    @instance = self
    @class2 = Class2.new(@instance)
  end
  def print_text(text)
    puts text
  end
end

class Class2
  def initialize(instance)
    @instance = instance
    @instance.print_text('Some text gets printed')
  end
end

# Run program
puts print_it = Class1.new

=begin
Result:
Some text gets printed
#<Class1:0x000001030aebb0>
=end

I don't seem to be able to get rid of the #<Class1:0x000001030aebb0> line.
I've tried to use to_s in several places to no avail (as shown in Dave
Thomas' book).

What am I doing wrong?

Thanks in advance.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;