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
- Create a new Ruby Application named Blog in the root of your user directory (it will be quicker to find using IRB)
- In main.rb, add the following:
- 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
- Point out the Rubyisms:
- end instead of closing brace (and it was pair matched for us)
- attr_accessor instead of getters and setters
- def for method definitions
- @ marks an instance variable
- Save the file.
IRB
- Start the IRB (from the top of the Window > Other menu)
- b = Blog.new( 'Ruby Blog' ) (fails)
- load 'main.rb' (fails)
- Dir.pwd (note you can use tab completion here)
- Dir.chdir( "Blog//lib" ) on Windows or ( "Blog/lib" ) on UNIX
- load 'main.rb'
- b = Blog.new( 'Ruby Blog' )
- b.title
- 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
- In IRB, load main.rb
- b.post - returns "There was no post method..."
- b.post - returns "Hello from the post method."
- Another way to add methods (do the following in IRB)
class Blog
def add_comment
puts "Comment added"
end
end
- b.add_comment
Demo Cleanup
- Delete the Blog application