From: 7stud -- Date: 2007-10-11T22:16:31+09:00 Subject: Re: Why does this use a block Anonymous wrote: > So I'm new to Ruby and I'm trying to use OptionParser to parse in some > command line arguments. In ruby, a lot of methods can be called with or without a block. When a method is called with a block, it may do some extra stuff. I think what the other posters are saying is that the initialize() method in OptionParser, which is called when you call new(), may be defined to do some extra things when it's called with a block. Here's an example of a method doing some extra things when it's called with a block: class Dog def age @age end def age=(num) @age = num end def initialize if block_given? yield self if @age == nil puts "You must set the dog's age." elsif @age > 20 puts "Error. That dog is dead." elsif @age < 0 puts "Error. That dog isn't born yet." end end end end d = Dog.new d.age = -3 puts "That dog's age is #{d.age}." d.age = 22 puts "That dog's age is #{d.age}." d = Dog.new do |dog| dog.age = 22 end --output:-- That dog's age is -3. That dog's age is 22. Error. That dog is dead. -- Posted via http://www.ruby-forum.com/.