def meth n
block_meth do
r = nil
if n == 42
r = ‘forty two’
else
r = ‘ask another question’
end
r
end
end
irb(main):018:0> x = meth 74
=> “ask another question”
irb(main):019:0> puts x
ask another question
=> nil
irb(main):020:0> y = meth 42
=> “forty two”
irb(main):021:0> puts y
forty two
=> nil
Is that what you are looking for?
Kirk Haines
···
On Fri, 13 Feb 2004, Ara.T.Howard wrote:
what i want to do is something like
value = meth 42
where
def meth n
block_meth do
if n == 42
return ‘forty-two’
else
return ‘ask another question’
end
end
end
def block_meth
yield
end
the problem is that you can’t ‘return’ from a block - so how is this done?
Date: Fri, 13 Feb 2004 15:08:34 +0900
From: nobu.nokada@softhome.net
Newsgroups: comp.lang.ruby
Subject: Re: return from yielded block
Hi,
At Fri, 13 Feb 2004 14:54:56 +0900,
Ara.T.Howard wrote in [ruby-talk:92771]:
the problem is that you can’t ‘return’ from a block - so how is this
done?
Return to where? The next statement of yield?
If so, you may want “next”.
return a value. i was not clear enough, let me try again:
def meth s
x =
block_meth do
if s =~ /forty/
return 40
else
return 0
end
end
x + 2
end
def block_meth; yield; end
p(meth(‘forty’)) # this prints 40 - i want it to print 42
Simply delete the “return”'s:
irb(main):001:0> def meth s
irb(main):002:1> x =
irb(main):003:1* block_meth do
irb(main):004:2* if s =~ /forty/
irb(main):005:3> 40
irb(main):006:3> else
irb(main):007:3* 0
irb(main):008:3> end
irb(main):009:2> end
irb(main):010:1> x + 2
irb(main):011:1> end
=> nil
irb(main):012:0>
irb(main):013:0* def block_meth; yield; end
=> nil
irb(main):014:0>
irb(main):015:0* p(meth(‘forty’)) # this prints 40 - i want it to print 42
42
=> nil
irb(main):016:0>
I guess you are aware of the fact that this kind of construction is a bit,
err, complicated and your real world code does a lot more in between…
irb(main):001:0> def block_meth
irb(main):002:1> yield
irb(main):003:1> end
=> nil
irb(main):004:0> def meth s
irb(main):005:1> x = block_meth do
irb(main):006:2* if s =~ /forty/
irb(main):007:3> r = 40
irb(main):008:3> else
irb(main):009:3* r = 0
irb(main):010:3> end
irb(main):011:2> r
irb(main):012:2> end
irb(main):013:1> x + 2
irb(main):014:1> end
=> nil
irb(main):015:0> puts meth(‘forty’)
42
=> nil
Is this what you want, or am I misunderstanding?
Kirk Haines
···
On Sat, 14 Feb 2004, Ara.T.Howard wrote:
return a value. i was not clear enough, let me try again:
def meth s
x =
block_meth do
if s =~ /forty/
return 40
else
return 0
end
end
x + 2
end
def block_meth; yield; end
p(meth(‘forty’)) # this prints 40 - i want it to print 42
The difference between art and science is that science is what we
understand well enough to explain to a computer.
Art is everything else.
– Donald Knuth, “Discover”
/bin/sh -c ‘for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done’
===============================================================================
nobu’s reply was the answer - your response works here but i also had a bunch
of stuff after the if/else/end : it was not the default return value. for
some reason i just didn’t explain myself well enough - sorry. the bottom line
is that i needed a way to prematurely ‘return’ a value from a yielded block
under $DEBUG/$NOOP conditions for testing…
cheers.
-a
···
On Fri, 13 Feb 2004, Robert Klemme wrote:
Date: Fri, 13 Feb 2004 17:04:33 +0100
From: Robert Klemme bob.news@gmx.net
Newsgroups: comp.lang.ruby
Subject: Re: return from yielded block
Date: Fri, 13 Feb 2004 15:08:34 +0900
From: nobu.nokada@softhome.net
Newsgroups: comp.lang.ruby
Subject: Re: return from yielded block
Hi,
At Fri, 13 Feb 2004 14:54:56 +0900,
Ara.T.Howard wrote in [ruby-talk:92771]:
the problem is that you can’t ‘return’ from a block - so how is this
done?
Return to where? The next statement of yield?
If so, you may want “next”.
return a value. i was not clear enough, let me try again:
def meth s
x =
block_meth do
if s =~ /forty/
return 40
else
return 0
end
end
x + 2
end
def block_meth; yield; end
p(meth(‘forty’)) # this prints 40 - i want it to print 42
Simply delete the “return”'s:
irb(main):001:0> def meth s
irb(main):002:1> x =
irb(main):003:1* block_meth do
irb(main):004:2* if s =~ /forty/
irb(main):005:3> 40
irb(main):006:3> else
irb(main):007:3* 0
irb(main):008:3> end
irb(main):009:2> end
irb(main):010:1> x + 2
irb(main):011:1> end
=> nil
irb(main):012:0>
irb(main):013:0* def block_meth; yield; end
=> nil
irb(main):014:0>
irb(main):015:0* p(meth(‘forty’)) # this prints 40 - i want it to print 42
42
=> nil
irb(main):016:0>
I guess you are aware of the fact that this kind of construction is a bit,
err, complicated and your real world code does a lot more in between…
–
EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================