Is there a cool way to do this without calling the function twice?:
a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)
···
--
Posted via http://www.ruby-forum.com/.
Is there a cool way to do this without calling the function twice?:
a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)
--
Posted via http://www.ruby-forum.com/.
Jonathan wrote:
Is there a cool way to do this without calling the function twice?:
a = func(b) unless func(b).nil? (AKA)
a = x unless (x = func(b)).nil?
a = func(b) if func(b)
a = x if (x = func(b)).
--
Ola Bini (http://ola-bini.blogspot.com) JvYAML, RbYAML, JRuby and Jatha contributor
System Developer, Karolinska Institutet (http://www.ki.se)
OLogix Consulting (http://www.ologix.com)
"Yields falsehood when quined" yields falsehood when quined.
Jonathan wrote:
Is there a cool way to do this without calling the function twice?:
a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)
a = func(b) || a
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
Jonathan wrote:
Is there a cool way to do this without calling the function twice?:
a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)
http://rubylution.ping.de/articles/2007/03/01/the-opposite-of-blank-in-rails
--
Florian Frank
Hi,
Am Freitag, 13. Apr 2007, 15:02:18 +0900 schrieb Jonathan:
Is there a cool way to do this without calling the function twice?:
a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)
As far as I see only the second statement makes a difference
when the return value is `false'. So,
a = func(b) || nil
would do!?
Bertram
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
Just to throw something obvious (which has not been mentioned so far) into the mix:
x = func(b)
a = x if x
robert
On 13.04.2007 08:02, Jonathan wrote:
Is there a cool way to do this without calling the function twice?:
a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)
Is there a cool way to do this without calling the function twice?:
a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)
In lisp there are libraries of utilities that provide what is called 'anaphoric' versions of control structures etc. They are simultaneously widely used and frowned upon. Paul Graham is one of the primary sources of code for this.
Ruby can't really do what lisp does, but here is a hacky approximation and some test cases. You'll see why they are frowned upon in the test cases (lisp doesn't do any better than ruby at avoiding errors).
Actually, what follows is the output of 'irb --prompt-mode xmp' so, should you want to actually use this stuff, you'll have to clean up the output. I don't think I recommend using it in Ruby by the way.
Cheers,
Bob
module Anaphoric
def ifa(v)
if v then
@it = v
yield
end
end
def ifa2(v)
if v then
yield(v)
end
end
end
==>nil
include Anaphoric
==>Object
ifa (2 + 3 + 4) do
puts "1 -- it #{@it}"
end
1 -- it 9
==>nil
ifa2 (2 + 3 + 4) do | it |
puts "2 -- it #{it}"
end
2 -- it 9
==>nil
ifa (2 + 3 + 4) do
puts "3a -- it #{@it}"
ifa (99 + 2 + 3 + 4) do
puts "3b -- it #{@it}"
end
puts "3c -- it #{@it}"
end
3a -- it 9
3b -- it 108
3c -- it 108
==>nil
ifa2 (2 + 3 + 4) do | it |
puts "4a -- it #{it}"
ifa2 (99 + 2 + 3 + 4) do | it |
puts "4b -- it #{it}"
end
puts "4c -- it #{it}"
end
4a -- it 9
4b -- it 108
4c -- it 108
==>nil
ifa2 (2 + 3 + 4) do | it |
puts "5a -- it #{it}"
ifa2 (99 + 2 + 3 + 4) do | it2 |
puts "5b -- it #{it2}"
end
puts "5c -- it #{it}"
end
5a -- it 9
5b -- it 108
5c -- it 9
==>nil
On 13-Apr-07, at 2:02 AM, Jonathan wrote:
--
Posted via http://www.ruby-forum.com/\.
----
Bob Hutchison -- tumblelog at <http://www.recursive.ca/so/>
Recursive Design Inc. -- <http://www.recursive.ca/>
xampl for Ruby -- <http://rubyforge.org/projects/xampl/>
assuming a is already defined
class Object; def yield; yield(self) end end
func(b).yield{|fb| a = fb if fb}
On Apr 13, 1:02 am, Jonathan <terho...@gmail.com> wrote:
Is there a cool way to do this without calling the function twice?:
a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)--
Posted viahttp://www.ruby-forum.com/.
Jonathan wrote:
> Is there a cool way to do this without calling the function twice?:
>
> a = func(b) unless func(b).nil? (AKA)
these two are not equivalent, just imagine func(b) returning false
> a = func(b) if func(b)
a = func(b) || a
and if a did not exist before?
Well it still works, as I expected given the poster of the message, but I really
feel this is not consistent behavior
@a = f(b) || @a
of course that nicely corresponds to the auto nilification of
undefined instance vars, like it or hate it.
But for local variables?
Cheers
Robert
On 4/13/07, Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw
> Is there a cool way to do this without calling the function twice?:
>
> a = func(b) unless func(b).nil? (AKA)
> a = func(b) if func(b)Just to throw something obvious (which has not been mentioned so far)
into the mix:x = func(b)
a = x if x
x = func b
a ||=x
How many combinations might we come up with ;)?
robert
Robert
On 4/13/07, Robert Klemme <shortcutter@googlemail.com> wrote:
On 13.04.2007 08:02, Jonathan wrote:
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw
Is there a cool way to do this without calling the function twice?:
>>
>> a = func(b) unless func(b).nil? (AKA)
>> a = func(b) if func(b)
greg wrote:
assuming a is already defined
class Object; def yield; yield(self) end end
func(b).yield{|fb| a = fb if fb}
Shades of forth:
class Object; def if?; yield(self) if self end end
a = 1
"fred wilma"[/fred/].if?{|x| a = x}
p a # ==> "fred"
"fred wilma"[/barney/].if?{|x| a = x}
p a # ==> "fred"
Btw, I think this construct has appeared before, but I can't seem to recall what it was called...
On Apr 13, 1:02 am, Jonathan <terho...@gmail.com> wrote:
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
Hi,
At Fri, 13 Apr 2007 17:57:47 +0900,
Robert Dober wrote in [ruby-talk:247783]:
> a = func(b) || a
and if a did not exist before?
a does exist at the assignment and initialized as nil.
--
Nobu Nakada
Certainly a lot more. However, my main point was to simply store the result of func(b) in another variable and reuse that. Sometimes the cool solutions are not without issues (see the other postings) and a pragmatic solution can be better at times.
Kind regards
robert
On 13.04.2007 13:07, Robert Dober wrote:
On 4/13/07, Robert Klemme <shortcutter@googlemail.com> wrote:
On 13.04.2007 08:02, Jonathan wrote:
> Is there a cool way to do this without calling the function twice?:
>
> a = func(b) unless func(b).nil? (AKA)
> a = func(b) if func(b)Just to throw something obvious (which has not been mentioned so far)
into the mix:x = func(b)
a = x if xx = func b
a ||=xHow many combinations might we come up with ;)?
# > On 13.04.2007 08:02, Jonathan wrote:
# > > a = func(b) unless func(b).nil? (AKA)
# > > a = func(b) if func(b)
# > x = func(b)
# > a = x if x
# x = func b
# a ||=x
irb
irb(main):001:0> a=1
=> 1
irb(main):002:0> a=func()
NoMethodError: undefined method `func' for main:Object
from (irb):2
irb(main):004:0> a=func() rescue a
=> 1
irb(main):006:0> b=func() rescue b
=> nil
From: Robert Dober [mailto:robert.dober@gmail.com]
# On 4/13/07, Robert Klemme <shortcutter@googlemail.com> wrote:
from :0
Thx Nobu, that is as a matter of fact a "logical" and "functional"
explanation, but my question was rather conceptional.
Do make it clear I will be a little bit blunt, forgive me.
I do not like that behavior!
And you ?
Cheers
Robert
On 4/13/07, Nobuyoshi Nakada <nobu@ruby-lang.org> wrote:
Hi,
At Fri, 13 Apr 2007 17:57:47 +0900,
Robert Dober wrote in [ruby-talk:247783]:
> > a = func(b) || a
> and if a did not exist before?a does exist at the assignment and initialized as nil.
--
Nobu Nakada
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw
Certainly a lot more. However, my main point was to simply store the
result of func(b) in another variable and reuse that. Sometimes the
cool solutions are not without issues (see the other postings) and a
pragmatic solution can be better at times.
Sure no argument here.
I just screwed up, I am touched that you have not even seen my *serious blunder*
a = x if x
is kind of *not*
a ||=x
Sorry Folks
Robert
On 4/13/07, Robert Klemme <shortcutter@googlemail.com> wrote:
On 13.04.2007 13:07, Robert Dober wrote:
Kind regards
robert
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw
*gg* I wanted to leave that for you.
Seriously, I was more focused on clarifying my intentions which I felt I did not make clear very well.
Kind regards
robert
On 13.04.2007 14:29, Robert Dober wrote:
On 4/13/07, Robert Klemme <shortcutter@googlemail.com> wrote:
On 13.04.2007 13:07, Robert Dober wrote:
Certainly a lot more. However, my main point was to simply store the
result of func(b) in another variable and reuse that. Sometimes the
cool solutions are not without issues (see the other postings) and a
pragmatic solution can be better at times.Sure no argument here.
I just screwed up, I am touched that you have not even seen my *serious blunder*
a = x if x
is kind of *not*
a ||=x
Sorry Folks
I suspect we've all made that mistake at one time or another
Ellie
Eleanor McHugh
Games With Brains
On 13 Apr 2007, at 13:29, Robert Dober wrote:
On 4/13/07, Robert Klemme <shortcutter@googlemail.com> wrote:
On 13.04.2007 13:07, Robert Dober wrote:
Certainly a lot more. However, my main point was to simply store the
result of func(b) in another variable and reuse that. Sometimes the
cool solutions are not without issues (see the other postings) and a
pragmatic solution can be better at times.Sure no argument here.
I just screwed up, I am touched that you have not even seen my *serious blunder*
a = x if x
is kind of *not*
a ||=x
----
raise ArgumentError unless @reality.responds_to? :reason
"Robert Dober" <robert.dober@gmail.com> writes:
Hi,
At Fri, 13 Apr 2007 17:57:47 +0900,
Robert Dober wrote in [ruby-talk:247783]:
> > a = func(b) || a
> and if a did not exist before?a does exist at the assignment and initialized as nil.
--
Nobu NakadaThx Nobu, that is as a matter of fact a "logical" and "functional"
explanation, but my question was rather conceptional.
Do make it clear I will be a little bit blunt, forgive me.
I do not like that behavior!
And you ?
It's very philosophical:
$ ruby -e 'thing = thing; p thing'
nil
On 4/13/07, Nobuyoshi Nakada <nobu@ruby-lang.org> wrote:
Cheers
Robert
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org
>> Certainly a lot more. However, my main point was to simply store the
>> result of func(b) in another variable and reuse that. Sometimes the
>> cool solutions are not without issues (see the other postings) and a
>> pragmatic solution can be better at times.
> Sure no argument here.
> I just screwed up, I am touched that you have not even seen my
> *serious blunder*
> a = x if x
> is kind of *not*
> a ||=xI suspect we've all made that mistake at one time or another
Really nice of you to say so, but to be honest, 99.99999% of the
people having made that mistake wisely refused to send it to the ML
R.
On 4/13/07, Eleanor McHugh <eleanor@games-with-brains.com> wrote:
On 13 Apr 2007, at 13:29, Robert Dober wrote:
> On 4/13/07, Robert Klemme <shortcutter@googlemail.com> wrote:
>> On 13.04.2007 13:07, Robert Dober wrote:Ellie
Eleanor McHugh
Games With Brains
----
raise ArgumentError unless @reality.responds_to? :reason
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw