gpt4 book ai didi

ruby-on-rails - 为什么 variable = true AND false 在 Rails 模型中导致值为 true 但在控制台中却没有

转载 作者:数据小太阳 更新时间:2023-10-29 08:20:40 27 4
gpt4 key购买 nike

为了确定照片是否符合指定的纵横比,我使用了这个:

exif2 = EXIFR::JPEG.new(photo.queued_for_write[:medium].path)
self.featured = exif2.width == 1250 and exif2.height == 833

然而,如果宽度为 1250 而高度为任意值,则返回 true。如果 exif2.height 不是 833,则在 Rails 控制台中运行这个确切的命令将返回 false。但是,将其更改为下面的 if 语句是可行的

if exif2.width == 1250 and exif2.height == 833
self.featured = true
else
self.featured = false
end

我不明白为什么,我正在尝试学习 Rails,所以我认为理解它对我很有用。

最佳答案

运算符优先级,and甚至低于=

所以你的第一个表达式被评估为

( self.featured = exif2.width == 1250 ) and ( exif2.height == 833 )

你的条件是这样计算的

if ( exif2.width == 1250 ) and ( exif2.height == 833 )

. . .表明您的不同答案虽然令人困惑,但并不是由于任何不一致造成的

尽可能使用&&代替

关于ruby-on-rails - 为什么 variable = true AND false 在 Rails 模型中导致值为 true 但在控制台中却没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15708658/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com