当然枚举在 Ruby 中不存在,但基于这篇文章,我使用了如下内容:
class PostType
Page = 1,
Post = 2
end
我想将值传递给一个方法并将其用于比较。所以:
initialize(post_type)
if post_type = PostType::Page
# do something here
elsif post_type = PostType::Post
# do something else here
end
end
但这不起作用,无论我将什么传递给我的类的构造函数,它总是产生相同的结果。
关于为什么将“假枚举”传递给方法并尝试比较它不起作用的任何想法?我必须比较价值吗?即post_type = 2
?