Syntax error in Ruby Cookbook example

Discussion in 'Ruby' started by JSU, Jun 16, 2009.

  1. JSU

    JSU Guest

    I am practicing/learning Ruby using Study Notes from www.ruby-lang.org.
    I am working on Validation program taken from Ruby Cookbook, but
    unable to run program as is. Please advise where I may have gone
    wrong.
    Thanks in advance. JSU




    # p050inputvalidation.rb
    # Here's an example from the Ruby Cookbook,
    # showing how one can do validation of user's inputs.
    class Name
    # Define default getter methods, but not setter methods.
    attr_reader :first, :last
    # When someone tries to set a first name,
    # enforce rules about it.
    def first=(first)
    if (first == nil or first.size == 0)
    raise ArgumentError.new('Everyone must have a first name.')
    end
    first = first.dup
    first[0] = first[0].chr.capitalize
    @first = first
    end

    # When someone tries to set a last name,
    # enforce rules about it.
    def last=(last)
    if (last == nil || last.size == 0)
    raise ArgumentError.new('Everyone must have a last name.')
    end
    last[0] = last[0].chr.capitalize
    @last = last
    end

    def full_name
    "#{@first} #{@last}"
    end
    # Delegate to the setter methods instead of
    # setting the instance variables directly.
    def initialize(first, last)
    self.first = first
    self.last = last
    end
    end

    jacob = Name.new('Jacob', 'Berendes')
    jacob.first = 'Mary Sue'
    jacob.full_name # => "Mary Sue Berendes"
    john = Name.new('John', 'Von Neumann')
    john.full_name # "John Von Neumann"
    john.first = 'john'
    john.first # => "john"
    john.first = nil
    # ArgumentError: Everyone must have a first name.
    Name.new('Kero, international football star and performance artist',
    nil)
    # ArgumentError: Everyone must have a last name.

    And my output is as follows:

    >ruby p050inputvalidation.rb

    p050inputvalidation.rb:11:in `first=': Everyone must have a first
    name. (ArgumentError)
    from p050inputvalidation.rb:46
    >Exit code: 1



    Thanks again!
    JSU, Jun 16, 2009
    #1
    1. Advertising

  2. Am Mittwoch 17 Juni 2009 00:00:05 schrieb JSU:
    > I am working on Validation program taken from Ruby Cookbook, but
    > unable to run program as is.


    =46rom what you've pasted it seems like you're getting exactly the result t=
    hat=20
    the comments indicate you should be getting, so I don't see a problem there.
    If you look at the comments, you'll notice that some of those lines are=20
    supposed to cause Exceptions.

    HTH,
    Sebastian
    Sebastian Hungerecker, Jun 17, 2009
    #2
    1. Advertising

  3. JSU

    JSU Guest

    On Jun 16, 4:30 pm, Sebastian Hungerecker <>
    wrote:
    > Am Mittwoch 17 Juni 2009 00:00:05 schrieb JSU:
    >
    > > I am working on Validation program taken from Ruby Cookbook, but
    > > unable to run program as is.

    >
    > From what you've pasted it seems like you're getting exactly the result that
    > the comments indicate you should be getting, so I don't see a problem there.
    > If you look at the comments, you'll notice that some of those lines are
    > supposed to cause Exceptions.
    >
    > HTH,
    > Sebastian


    Thanks Sebastian. I suppose I was thrown because it doesn't look all
    tidy like the other examples. Cheers. JSU
    JSU, Jun 17, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    338
  2. Steve Howell

    iterblocks cookbook example

    Steve Howell, Jun 2, 2007, in forum: Python
    Replies:
    3
    Views:
    237
    Gerard Flanagan
    Jun 4, 2007
  3. kj
    Replies:
    4
    Views:
    279
    Tim Roberts
    Jan 12, 2008
  4. Randy Lawrence

    Free Ruby "Cookbook" (48.71% done)

    Randy Lawrence, Jul 15, 2004, in forum: Ruby
    Replies:
    11
    Views:
    215
    Karsten Meier
    Jul 18, 2004
  5. Paul Gillingwater
    Replies:
    0
    Views:
    108
    Paul Gillingwater
    Apr 6, 2004
Loading...

Share This Page