Forum: Ruby on Rails ruby equivalent of isset()

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jeroen H. (Guest)
on 2006-02-01 23:11
(Received via mailing list)
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
Jules J. (Guest)
on 2006-02-01 23:17
Jeroen H. 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?
>
> Jeroen

The default value is nil, and thus false.

if myvar
  #some code
else
  #default behaviour
end

Works.

Jules.
Bob S. (Guest)
on 2006-02-01 23:21
(Received via mailing list)
Not positive but Rails provides a blank? method. Check it out.

Bob S.
http://www.railtie.net/
Rick O. (Guest)
on 2006-02-01 23:24
(Received via mailing list)
> 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 O.
http://techno-weenie.net
Jules J. (Guest)
on 2006-02-01 23:25
irb(main):001:0> hash = {'a' => 1, 'b' => 2}
=> {"a"=>1, "b"=>2}
irb(main):003:0> hash['c']
=> nil
irb(main):004:0>
Jeroen H. (Guest)
on 2006-02-01 23:30
(Received via mailing list)
Rick O. wrote:
> 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
>   #
> end

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?

Jeoen
Jeroen H. (Guest)
on 2006-02-01 23:33
(Received via mailing list)
Jules J. 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
 >>
Phillip H. (Guest)
on 2006-02-02 01:50
(Received via mailing list)
On 2/02/2006, at 10:25 AM, Jeroen H. 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.
Bob S. (Guest)
on 2006-02-02 05:34
(Received via mailing list)
defined? is an operator just like &&, and, or etc...

Page 88 of the Pickaxe (116 in the PDF)

Bob S.
http://www.railtie.net/
Mukto M. (Guest)
on 2016-03-23 07:38
Rick O. wrote in post #29743:
>> 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 O.
> http://techno-weenie.net

     Effective Answer

     Mukto
This topic is locked and can not be replied to.