From: Dale Martenson Date: 2002-04-18T04:18:21+09:00 Subject: RE: Ways methods return values This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C1E643.B1987300 Content-Type: text/plain; charset="iso-8859-1" > -----Original Message----- > From: Hal E. Fulton [mailto:hal9000@hypermetrics.com] > > ----- Original Message ----- > From: "Sean O'Dell" > > > I know in Ruby, the last evaluation is returned by a method > and you can > also > > explicitly "return" a value, but I was wondering if there > was a way to do > > something I became accustomed to while programming in Pascal. > > > > In Pascal, functions have a local variable with the same name of the > > function that you can assign to at any location within the > function and no > > matter what causes the function to return (aside from an > exception), the > > return value will be whatever you assigned to that variable. > > [snip] > > This is interesting... I'm a Pascal oldtimer myself, > and I can understand the motivation for this. > > However, it's not obvious to me there's a way to > do it in Ruby. > > There are future plans for a way to "hook" into the > entry and exit of a method, if I understand correctly... > that might be a useful line of research for a future > way to do this... that's assuming the reflection API > has a way to retrieve local vars for a method, which > I can't recall at the moment. > While not elegant, this works: def pascallike(parameter) begin result = 1 result = 2 if parameter == 1 # intervening silly code -- assumed to be pages and pages of golden code print "parameter is #{parameter}\n" i = parameter * 2; print "i is #{i}\n" # The "ensure" clause is always going to be executed no matter what. ensure return result end end print "First call:\n" result = pascallike(0) print "result is #{result}\n" print "Second call:\n" result = pascallike(1) print "result is #{result}\n" ------_=_NextPart_001_01C1E643.B1987300 Content-Type: text/html; charset="iso-8859-1" RE: Ways methods return values

> -----Original Message-----
> From: Hal E. Fulton [mailto:hal9000@hypermetrics.com]
>
> ----- Original Message -----
> From: "Sean O'Dell" <sean@celsoft.com>
>
> > I know in Ruby, the last evaluation is returned by a method
> and you can
> also
> > explicitly "return" a value, but I was wondering if there
> was a way to do
> > something I became accustomed to while programming in Pascal.
> >
> > In Pascal, functions have a local variable with the same name of the
> > function that you can assign to at any location within the
> function and no
> > matter what causes the function to return (aside from an
> exception), the
> > return value will be whatever you assigned to that variable.
>
> [snip]
>
> This is interesting... I'm a Pascal oldtimer myself,
> and I can understand the motivation for this.
>
> However, it's not obvious to me there's a way to
> do it in Ruby.
>
> There are future plans for a way to "hook" into the
> entry and exit of a method, if I understand correctly...
> that might be a useful line of research for a future
> way to do this... that's assuming the reflection API
> has a way to retrieve local vars for a method, which
> I can't recall at the moment.
>

While not elegant, this works:

def pascallike(parameter)
  begin
    result = 1
 
    result = 2 if parameter == 1

    # intervening silly code -- assumed to be pages and pages of golden code
    print "parameter is #{parameter}\n"
    i = parameter * 2;
    print "i is #{i}\n"

  # The "ensure" clause is always going to be executed no matter what.
  ensure
    return result
  end
end 

print "First call:\n"
result = pascallike(0)
print "result is #{result}\n"

print "Second call:\n"
result = pascallike(1)
print "result is #{result}\n"

------_=_NextPart_001_01C1E643.B1987300--