Hi, Is there a way to test if a certain (local) variable has been initialized? I''d like to do something like if isset(myvar) and myvar.true? #some code else #default behaviour end I have a lot of shared views that I call with params such as { show_pager => true Instead of having to explicitly say show_pager => false it should be possible to make false a default value, right? Jeroen
Jeroen Houben wrote:> Hi, > > Is there a way to test if a certain (local) variable has been > initialized? > > I''d like to do something like > > if isset(myvar) and myvar.true? > #some code > else > #default behaviour > end > > I have a lot of shared views that I call with params such as { > show_pager => true > > Instead of having to explicitly say show_pager => false it should be > possible to make false a default value, right? > > JeroenThe default value is nil, and thus false. if myvar #some code else #default behaviour end Works. Jules. -- Posted via http://www.ruby-forum.com/.
Not positive but Rails provides a blank? method. Check it out. Bob Silva http://www.railtie.net/ -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Jeroen Houben Sent: Wednesday, February 01, 2006 1:10 PM To: rails@lists.rubyonrails.org Subject: [Rails] ruby equivalent of isset() Hi, Is there a way to test if a certain (local) variable has been initialized? I''d like to do something like if isset(myvar) and myvar.true? #some code else #default behaviour end I have a lot of shared views that I call with params such as { show_pager => true Instead of having to explicitly say show_pager => false it should be possible to make false a default value, right? Jeroen _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
> The default value is nil, and thus false. > > if myvar > #some code > else > #default behaviour > end > > Works.That works with @instance vars, but not local vars. You''ll get a NameError. You can do this, however: if defined?(my_var) # else # end -- Rick Olson http://techno-weenie.net
irb(main):001:0> hash = {''a'' => 1, ''b'' => 2} => {"a"=>1, "b"=>2} irb(main):003:0> hash[''c''] => nil irb(main):004:0> -- Posted via http://www.ruby-forum.com/.
Rick Olson wrote:>> The default value is nil, and thus false. >> >> if myvar >> #some code >> else >> #default behaviour >> end >> >> Works. > > That works with @instance vars, but not local vars. You''ll get a > NameError.Exactly.> You can do this, however: > > if defined?(my_var) > # > else > # > endGreat, this is what I was looking for. I did look in the Ruby docs, but could find anything under class Object, which class does defined? belong to? Jeoen
Jules Jacobs wrote:> irb(main):001:0> hash = {''a'' => 1, ''b'' => 2} > => {"a"=>1, "b"=>2} > irb(main):003:0> hash[''c''] > => nil > irb(main):004:0> >But this hash has already been initialized. I meant for local possibly uninitialized vars. >> if myvar then puts ''true'' else puts ''false'' end NameError: undefined local variable or method `myvar'' for #<Object:0x13d9b8> from (irb):2 >>
On 2/02/2006, at 10:25 AM, Jeroen Houben wrote:> Great, this is what I was looking for. I did look in the Ruby docs, > but could find anything under class Object, which class does > defined? belong to?It''s a language thing, it doesn''t belong to any object. Not as far as I can grep anyway. -- Phillip Hutchings phillip.hutchings@sitharus.com http://www.sitharus.com/ -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2234 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060201/973967c6/smime.bin
defined? is an operator just like &&, and, or etc... Page 88 of the Pickaxe (116 in the PDF) Bob Silva http://www.railtie.net/> -----Original Message----- > From: rails-bounces@lists.rubyonrails.org [mailto:rails- > bounces@lists.rubyonrails.org] On Behalf Of Phillip Hutchings > Sent: Wednesday, February 01, 2006 3:48 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Re: ruby equivalent of isset() > > > On 2/02/2006, at 10:25 AM, Jeroen Houben wrote: > > > Great, this is what I was looking for. I did look in the Ruby docs, > > but could find anything under class Object, which class does > > defined? belong to? > > It''s a language thing, it doesn''t belong to any object. Not as far as > I can grep anyway. > > > -- > Phillip Hutchings > phillip.hutchings@sitharus.com > http://www.sitharus.com/ >