Iván Vega Rivera
2005-Nov-24 20:25 UTC
An elegant way to check for nil and then check the value
Hi, I''m sure there''s an elegant way of doing the following: def ep_is_sysop sysop = false if not @session[''user''].nil? sysop = true if @session[''user''].role == ''siteop'' end sysop end Could you help me out? Thanks! Ivan V.
Craig Webster
2005-Nov-24 20:27 UTC
Re: An elegant way to check for nil and then check the value
On 24 Nov 2005, at 20:25, Iván Vega Rivera wrote:> I''m sure there''s an elegant way of doing the following: > > def ep_is_sysop > sysop = false > if not @session[''user''].nil? > sysop = true if @session[''user''].role == ''siteop'' > end > sysop > endBit new to Ruby & Rails so I''m probably wrong, but I''d do def ep_is_sysop @session[''user''] && @session[''user''].role == ''siteop'' end Yours, Craig -- Craig Webster | t: +44 (0)131 516 8595 | e: craig-07VhxHapISisTnJN9+BGXg@public.gmane.org Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net
I don''t know if this is any prettier but should be able to do: (untested) def ep_is_sysop @session[''user''] and @session[''user''].role == ''siteop'' end
Hendie Dijkman
2005-Nov-24 21:35 UTC
Re: An elegant way to check for nil and then check the value
How about def ep_is_sysop @session[''user''] == ''siteop'' end nil compared to anythng will always yield false. qed On 11/24/05, Robert <mannl-KK0ffGbhmjU@public.gmane.org> wrote:> I don''t know if this is any prettier but should be able to do: > (untested) > > def ep_is_sysop > @session[''user''] and @session[''user''].role == ''siteop'' > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Hendie Dijkman
2005-Nov-24 21:36 UTC
Re: An elegant way to check for nil and then check the value
<redface> Oops </redface>
Iván Vega Rivera
2005-Nov-24 22:10 UTC
Re: An elegant way to check for nil and then check the value
D''oh... Ruby is so simple and intuitive (for a "empty" brain that is) that it actually makes it really hard for me... I''m always trying to do stuff like I''d do in PHP or in other languages. Thanks Craig & Robert. Hendie, that''s what I was doing at first, but I think you''ve already realized why it doesn''t always work :-) Ivan V. Craig Webster wrote:> On 24 Nov 2005, at 20:25, Iván Vega Rivera wrote: >> I''m sure there''s an elegant way of doing the following: >> >> def ep_is_sysop >> sysop = false >> if not @session[''user''].nil? >> sysop = true if @session[''user''].role == ''siteop'' >> end >> sysop >> end > > Bit new to Ruby & Rails so I''m probably wrong, but I''d do > > def ep_is_sysop > @session[''user''] && @session[''user''].role == ''siteop'' > end > > Yours, > Craig > -- > Craig Webster | t: +44 (0)131 516 8595 | e: craig-07VhxHapISisTnJN9+BGXg@public.gmane.org > Xeriom.NET | f: +44 (0)709 287 1902 | w: http://xeriom.net > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Tudor Oprea
2005-Nov-24 23:33 UTC
Re: An elegant way to check for nil and then check the value
ivanvr wrote:> I''m sure there''s an elegant way of doing the following: > > def ep_is_sysop > sysop = false > if not @session[''user''].nil? > sysop = true if @session[''user''].role == ''siteop'' > end > sysop > endThe answer was already given, but it may be worthwhile to check out http://www.rubygarden.org/ruby?RubyIdioms which addresses this, and many other useful bits. Cheers, -Tudor -- Posted via http://www.ruby-forum.com/.
Iván Vega Rivera
2005-Nov-25 00:47 UTC
Re: Re: An elegant way to check for nil and then check the value
Tudor Oprea wrote:> ivanvr wrote: > >> I''m sure there''s an elegant way of doing the following: >> >> def ep_is_sysop >> sysop = false >> if not @session[''user''].nil? >> sysop = true if @session[''user''].role == ''siteop'' >> end >> sysop >> end >> > > The answer was already given, but it may be worthwhile to check out > > http://www.rubygarden.org/ruby?RubyIdioms > > which addresses this, and many other useful bits. > > Cheers, > -Tudor > > >Hey, that''s pretty useful information! Thanks! Ivan V.
Steve Ross
2005-Nov-25 03:23 UTC
Re: Re: An elegant way to check for nil and then check the value
Using boolean short-circuit operators is not language specific. It''s more idiomatic. So, while you might be more likely to write the code below in PHP, it would also work if you wrote: function ep_is_sysop(){ return (!empty($_SESSION[''user''] && $_SESSION[''user''] == ''siteop''); } That''s not to say you should use PHP. Just that you can use some similar constructs to this. cheers On 11/24/05 4:47 PM, "Iván Vega Rivera" <ivanvega-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Tudor Oprea wrote: >> ivanvr wrote: >> >>> I''m sure there''s an elegant way of doing the following: >>> >>> def ep_is_sysop >>> sysop = false >>> if not @session[''user''].nil? >>> sysop = true if @session[''user''].role == ''siteop'' >>> end >>> sysop >>> end >>> >> >> The answer was already given, but it may be worthwhile to check out >> >> http://www.rubygarden.org/ruby?RubyIdioms >> >> which addresses this, and many other useful bits. >> >> Cheers, >> -Tudor >> >> >> > Hey, that''s pretty useful information! Thanks! > > Ivan V. > >