BSalunke
BSalunke

Reputation: 11727

What does following is mean in ruby?

Can any one tell me? what does following means in ruby program:

obj = myClass.new
(Err("Error: Can't get myClass instance"); exit) if obj == nil

Thanks in advance

Upvotes: 0

Views: 124

Answers (3)

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

This is equivalent to:

obj = myClass.new
if obj == nil
   Err("Error: Can't get myClass instance")
   exit
end

I would personally use the version I show above as I consider it more readable.

Upvotes: 3

lukad
lukad

Reputation: 17863

It means that Err("Error: Can't get myClass instance") is called followed by exit if obj is nil.

In ruby you can write an if statement like this: (code) if (expression).

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230336

# create instance of a class with non-standard name. 
obj = myClass.new
# call function Err and exit if myClass.new returned nil
(Err("Error: Can't get myClass instance"); exit) if obj == nil

I find this code confusing. Under normal circumstances, new never returns nil. If it does in your app, then you have much more complicated code somewhere. This one is not your biggest problem :)

Upvotes: 1

Related Questions