Depends on your assumptions on what value this variable can hold. If it is
always a String, then
@name = 'Guest' if @name.empty?

If it can be a nil or a String, then
@name = 'Guest' if @name.nil? || @name.empty?

With rails/activesupport it can be shortened to
@name = @name.presence || 'Guest'
http://api.rubyonrails.org/classes/Object.html#method-i-presence

@name ||= 'Guest' is not what you want if @name can hold a non-nil value
http://ruby-doc.org/core-2.3.3/doc/syntax/assignment_rdoc.html#label-Abbreviated+Assignment

On Thu, Nov 24, 2016 at 12:24:15PM +0400, Nathan Guilty wrote:
> Hi,
> 
> Is there a better way to test if a variable|instance is empty ?
> I usually use :
> 
> @name = 'Guest' if @name.empty?
> 
> There's no way to have a trick like the following ?
> @name ||= 'Guest' # except here @name need to be 'nil' or 'false' not empty
> 
> Thank you very much for you replies.
> 
> /Nathan
> 
> Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>