Quick newbie true/false/nil question

I ran across this line of code today, and it doesn't make sense to me.

  if ((klass = CommDispatcher.check_hash(path)) == nil)

How would that value ever equal nil? I thought that if klass failed to be
assigned to the value of CommDispatcher.check_hash(path) then the value
would be false and if it was assigned to the value of
CommDispatcher.check_hash(path) it would be true.. How would it be nil?

Thanks!

I'm not familiar with the CommDispatcher class, but it looks like
klass will be assigned whatever the return value of check_hash()
returns, whether that's nil or false or something else.

Jeff

···

On Feb 13, 2:24 pm, "Jason Mayer" <slam...@gmail.com> wrote:

I ran across this line of code today, and it doesn't make sense to me.

  if ((klass = CommDispatcher.check_hash(path)) == nil)

How would that value ever equal nil? I thought that if klass failed to be
assigned to the value of CommDispatcher.check_hash(path) then the value
would be false and if it was assigned to the value of
CommDispatcher.check_hash(path) it would be true.. How would it be nil?

Thanks!

Jason Mayer wrote:

I ran across this line of code today, and it doesn't make sense to me.

  if ((klass = CommDispatcher.check_hash(path)) == nil)

I don't know anything about CommDispatcher, but I'd assume you could
rewrite the above code like so:

if (!CommDispatcher.check_hash(path))

The code above seems like a pretty bad code example.

-Drew

···

--
Posted via http://www.ruby-forum.com/\.

Right... but isn't the assignment of the value 'nil' going to return true
since the assignment succeeded? I remember screwing up a program by
forgetting to put in the extra = sign once... my if blah = blah2 statement
always returned true because the assignment occurred.

···

On 2/13/07, Jeff <cohen.jeff@gmail.com> wrote:

On Feb 13, 2:24 pm, "Jason Mayer" <slam...@gmail.com> wrote:
> I ran across this line of code today, and it doesn't make sense to me.
>
> if ((klass = CommDispatcher.check_hash(path)) == nil)
>
> How would that value ever equal nil? I thought that if klass failed to
be
> assigned to the value of CommDispatcher.check_hash(path) then the value
> would be false and if it was assigned to the value of
> CommDispatcher.check_hash(path) it would be true.. How would it be nil?
>
> Thanks!

I'm not familiar with the CommDispatcher class, but it looks like
klass will be assigned whatever the return value of check_hash()
returns, whether that's nil or false or something else.

Jeff

The above code is certainly not a nice-looking one but that was not
the question.

I suppose that the puzzling part was that the variable klass will be
assigned with return value from CommDispatcher.check_hash(path) and
that that value could be nil.
It is true, if we presume that it is a possible return value from
CommDispatcher.check_hash method.

The assignment inside the if condition is not a good but it is a
common construction especially if you come form C++ world.
Ruby has far more interesting constructions but it all depends on a
context and mostly on programming style.

Generally this assignment inside the if condition should be avoided.

Dima

···

On Feb 13, 9:39 pm, Drew Olson <olso...@gmail.com> wrote:

Jason Mayer wrote:
> I ran across this line of code today, and it doesn't make sense to me.

> if ((klass = CommDispatcher.check_hash(path)) == nil)

I don't know anything about CommDispatcher, but I'd assume you could
rewrite the above code like so:

if (!CommDispatcher.check_hash(path))

The code above seems like a pretty bad code example.

-Drew

--
Posted viahttp://www.ruby-forum.com/.

Like in C, assignment returns IIRC the result of the assignment, so:

( (a = 2+3) == 5 ) == true

···

On 2/13/07, Jason Mayer <slamboy@gmail.com> wrote:

On 2/13/07, Jeff <cohen.jeff@gmail.com> wrote:
>
> On Feb 13, 2:24 pm, "Jason Mayer" <slam...@gmail.com> wrote:
> > I ran across this line of code today, and it doesn't make sense to me.
> >
> > if ((klass = CommDispatcher.check_hash(path)) == nil)
> >
> > How would that value ever equal nil? I thought that if klass failed
to
> be
> > assigned to the value of CommDispatcher.check_hash(path) then the
value
> > would be false and if it was assigned to the value of
> > CommDispatcher.check_hash(path) it would be true.. How would it be
nil?
> >
> > Thanks!
>
> I'm not familiar with the CommDispatcher class, but it looks like
> klass will be assigned whatever the return value of check_hash()
> returns, whether that's nil or false or something else.
>
> Jeff

Right... but isn't the assignment of the value 'nil' going to return true
since the assignment succeeded? I remember screwing up a program by
forgetting to put in the extra = sign once... my if blah = blah2 statement
always returned true because the assignment occurred.

The value associated with an assignment statement is the value of
the right hand side of the assignment.

It is somewhat nonsensical to ask whether an assignment 'succeeds' or not.
The only way for an assignment to fail would be a syntax error or some
sort of exception while evaluating the right hand side, in which case the assignment won't be evaluated as an expression at all.

It may help to remember that nil is simply a literal reference to a
particular object in the same way that 1 is a literal reference for a
particular Fixnum object (the one that behaves like the number 1).

Don't think of nil as meaning the absence of any object but instead
think of it as the unique object known as nil.

Gary Wright

···

On Feb 13, 2007, at 3:34 PM, Jason Mayer wrote:

Right... but isn't the assignment of the value 'nil' going to return true
since the assignment succeeded? I remember screwing up a program by
forgetting to put in the extra = sign once... my if blah = blah2 statement
always returned true because the assignment occurred.

heh. I should just test it for myself before posting to the mailing list.

var1 = 1
var2 = 2
if var1 = var2
  puts "First if statement = true: #{var1} = #{var2}"
else
  puts "First if statement = false: #{var1} != #{var2}"
end
var1 = 1
var2 = 2
if ((var1 = var2) == true)
  puts "Second if statement = true: #{var1} = #{var2} = true"
else
  puts "Second if statement = false: #{var1} = #{var2} != true"
end
var1 = 1
var2 = 2
if ((var1 = var2) == nil)
  puts "Third if statement = true: #{var1} = #{var2} = nil "
else
  puts "Third if statement = false: #{var1} = #{var2} != nil"
end
var1 = 1
var2 = nil
if ((var1 = var2) == nil)
  puts "Fourth if statement = true: #{var1} = #{var2} = nil "
else
  puts "Fourth if statement = false: #{var1} = #{var2} != nil"
end
#output
First if statement = true: 2 = 2
Second if statement = false: 2 = 2 != true
Third if statement = false: 2 = 2 != nil
Fourth if statement = true: = = nil

···

On 2/13/07, SonOfLilit <sonoflilit@gmail.com> wrote:

Like in C, assignment returns IIRC the result of the assignment, so:

( (a = 2+3) == 5 ) == true