NBDemoGettingStarted

Getting Started with Ruby (5 Minutes)

Description

This Ruby shows how to get started with Ruby showing the power of the editor and using JRuby.

Product Versions Supported

NetBeans 6.0 Beta 1 and higher

Points to Hit

  • Syntax highlighting
  • CTL-R instant renaming
  • code completion
  • help documentation

Demo Prep

  1. Download and extract FreeTTS. Only the binaries are needed.
  2. Copy the jars from the FreeTTS lib directory to your JRuby lib directory. This directory will be "netbeans_install_dir/ruby1/jruby-1.0.1/lib/"

Gotchas

None known at this point

Demo

1. Create a new Ruby Application named SimpleRuby

2. Type the following code in Main.rb and hit return.


  class  Greeter

3. Show how the "end" was generated for the class

Explanation Point:
Now we need to add a couple of methods


4. Type the following method into the Greeter class


  def initialize(inName)

5. Hit return. Inside the method type the following code.


  @name = inName.

6. Show the code completion and choose the "capitalize" method.

7. Show the help documentation for the capitalize method.

8. Add this method as well to the Greeter class.


def salute

9. Add the following code to the salute method.



puts "Hello #

10. Show code completion on the # .

11. Inside the #{} type @n and let the code completion choose "@name Greeter". Make sure to add the following ".

12. After the Greeter class, add the following code.



g = Greeter.new("david")
g.salute()

13. Run the Ruby application and show the output.

Debugging

14. Set a breakpoint on the following line.


    puts "Hello #{@name}"

15. Debug the Ruby Application and show how the program stops on the line.

16. Switch to the "Local Variables" window and show the "@name" variable.

The power of JRuby

17. At the top of the file (before the class definition) type jc<Tab> to expand the require 'java' template. Complete the code as follows:

  require 'java'
  include_class 'com.sun.speech.freetts.Voice'
  include_class 'com.sun.speech.freetts.VoiceManager'

18. Comment out the following line in the salute method.


    puts "Hello #{@name}"


19. Put the following code below the commented out line.

  
    # Get a voice
    voice = VoiceManager.getInstance.getVoice('kevin16')
    # Allocate a resource for the voice
    voice.allocate
    voice.speak(@name)
   


20. Run the Ruby Application with the volume turned up.

Demo Cleanup

None.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo