Forums RubyCocoa

 
96_small Frantz Gauthier 1 post

Just a simple question about this piece of code :


class SpeechController < NSObject
  attr_reader :number_of_utterances   # (4)

  def init
    super_init
    @synthesizer = NSSpeechSynthesizer.alloc.init
    @number_of_utterances = 0   # (5)
    self
  end

  def initialize
    @synthesizer = NSSpeechSynthesizer.alloc.init
  end

...

Why instanciate NSSpeechSynthesizer both in int and initialize methods ?

 
England-small_small Brian Marick 32 posts

That looks like a leftover. Probably I was using this solution to explain the difference between RubyCocoa’s alloc.init() and pure Ruby’s initialize(). If you remove the initialize() you’ll see that all works fine.

I’ve changed the code; it’ll appear with the next beta.

Thanks.

 
Calcite_small Frederick C.... 7 posts

In the following code, you mistakenly refer to the class

SpeechController

in
 @speech_controller.number_of_utterances >= 2  # (3) 

Shouldn’t ‘speech_controller’ be ‘SpeechController’?

#!/usr/bin/env ruby
#---
# Excerpted from "RubyCocoa",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material, 
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose. 
# Visit <a href="http://www.pragmaticprogrammer.com/titles/bmrc">http://www.pragmaticprogrammer.com/titles/bmrc</a> for more book information.
#---

require 'osx/cocoa'
include OSX

class App < NSObject
  def applicationDidFinishLaunching(aNotification)
    statusbar = NSStatusBar.systemStatusBar
    item = statusbar.statusItemWithLength(NSVariableStatusItemLength)
    image = NSImage.alloc.initWithContentsOfFile("stretch.tiff")
    item.setImage(image)
    @speech_controller = SpeechController.alloc.init  # (1) 
    @speech_controller.add_menu_to(item)  # (2) 
  end

  def applicationShouldTerminate(sender)
    @speech_controller.number_of_utterances >= 2  # (3) 
  end
end

class SpeechController < NSObject
  attr_reader :number_of_utterances   # (4)

  def init
    super_init
    @synthesizer = NSSpeechSynthesizer.alloc.init
    @number_of_utterances = 0   # (5)
    self
  end

  def initialize
    @synthesizer = NSSpeechSynthesizer.alloc.init
  end

  def add_menu_to(container)           
    menu = NSMenu.alloc.init           
    container.setMenu(menu)

    item = menu.addItemWithTitle_action_keyEquivalent("Speak", "speak", '')
    item.setTarget(self)          

    item = menu.addItemWithTitle_action_keyEquivalent("Quit", "terminate:", 'q')   
    item.setKeyEquivalentModifierMask(NSCommandKeyMask) 
    item.setTarget(NSApp)         
  end

  def speak(sender)     
    @synthesizer.startSpeakingString("I have nothing to say.")
    @number_of_utterances += 1   # (6)
  end
end

NSApplication.sharedApplication
NSApp.setDelegate(App.alloc.init)
NSApp.run
 
England-small_small Brian Marick 32 posts

It’s correct, I think. SpeechController is the class; @speech_controller is an instance created from SpeechController.

4 posts, 3 voices