RubyDemo - NetBeans Wiki

RubyDemo

Ruby Language Demo Demo (5 Minutes)

Description

This demo introduces the dynamic nature of the Ruby language to the audience

Product Versions Supported

NetBeans 6.0

Points to Hit

  • Code completion and integrated RDoc
  • Pair matching
  • Live code templates
  • IRB Integration


Demo Prep

None

Gotchas

NetBeans 6.0 isn't even at Beta, so anything could go wrong :-)

Demo

Ruby Editor

  1. Create a new Ruby Application named Blog in the root of your user directory (it will be quicker to find using IRB)
  2. In main.rb, add the following:
  3. Type cl and press Ctrl+Space to show the code completion and RDoc. Show how you can navigate to the Ruby source. Select class and then complete the rest of the class:
     
  class Blog
    attr_accessor :title
       
    def initialize( title )
      @title = title
    end        
  end
  1. Point out the Rubyisms:
    1. end instead of closing brace (and it was pair matched for us)
    2. attr_accessor instead of getters and setters
    3. def for method definitions
    4. @ marks an instance variable
  1. Save the file.

IRB

  1. Start the IRB (from the top of the Window > Other menu)
  2. b = Blog.new( 'Ruby Blog' ) (fails)
  3. load 'main.rb' (fails)
  4. Dir.pwd (note you can use tab completion here)
  5. Dir.chdir( "Blog//lib" ) on Windows or ( "Blog/lib" ) on UNIX
  6. load 'main.rb'
  7. b = Blog.new( 'Ruby Blog' )
  8. b.title

Method Missing & Metaprogramming

  1. Add the following method to main.rb (hint: put the code in a template):
  def method_missing( method )
    puts "There was no #{method} method, but please try again."

    Blog.class_eval do
      define_method  method do
        puts "Hello from the #{method} method."
      end
    end
  end
  1. In IRB, load main.rb
  2. b.post - returns "There was no post method..."
  3. b.post - returns "Hello from the post method."
  4. Another way to add methods (do the following in IRB)
  class Blog
    def add_comment
      puts "Comment added"
    end
  end
  1. b.add_comment

Demo Cleanup

  • Delete the Blog application
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