ruby 方法返回 true 或 false

发布于 2024-12-29 21:58:01 字数 748 浏览 1 评论 0原文

如果每个帖子都被某个人关注,我想从 ruby​​ 方法中获取 true ,如果没有则获取 false 。

我有这个方法:

def number_of_posts_that_are_followed
  user_to_be_followed = User.find(params[:id]) #users whose posts, will be followed by another user
  user_to_be_followed.posts.each do |this_post| 
    if current_user.follows?(this_board) == true #method that returns true if the current_user is following this post of the user whose posts will be followed
     return true
    else
     return false
    end 
   end
  end

问题是,如果第一篇文章(在第一次迭代中)后面跟着 current_user,则该方法返回 true。如果每个帖子都被关注,我希望返回 true,如果没有被关注,则返回 false。

我尝试过这样计数:

count = user_to_be_followed.posts.count

I want get from a method ruby true if every posts are followed for a people and false if not.

I have this method:

def number_of_posts_that_are_followed
  user_to_be_followed = User.find(params[:id]) #users whose posts, will be followed by another user
  user_to_be_followed.posts.each do |this_post| 
    if current_user.follows?(this_board) == true #method that returns true if the current_user is following this post of the user whose posts will be followed
     return true
    else
     return false
    end 
   end
  end

The problem is that this method return true if the first post (in the first iteration) its followed by current_user. I want return true if every posts are followed or false if not are followed.

I have tried put a count like this:

count = user_to_be_followed.posts.count

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走过海棠暮 2025-01-05 21:58:01

您应该使用Enumerable#all? 方法检查列表的所有元素是否匹配谓词(返回布尔值的块)中定义的条件。

全部? [{|对象|块}] → 正确或错误

将集合的每个元素传递到给定的块。方法
如果块从不返回 false 或 nil,则返回 true。如果块是
没有给出,Ruby 添加了一个隐式块 {|obj| obj}(仅此而已?
仅当集合成员均不为 false 或
无。)

def number_of_posts_that_are_followed
  User.find(params[:id]).posts.all? {|post| current_user.follows? post }
end

You should use Enumerable#all? method to check that all elements of the list match condition defined in predicate (block that returns boolean value).

all? [{|obj| block } ] → true or false

Passes each element of the collection to the given block. The method
returns true if the block never returns false or nil. If the block is
not given, Ruby adds an implicit block of {|obj| obj} (that is all?
will return true only if none of the collection members are false or
nil.)

def number_of_posts_that_are_followed
  User.find(params[:id]).posts.all? {|post| current_user.follows? post }
end
别念他 2025-01-05 21:58:01
def number_of_posts_that_are_followed
  user_to_be_followed = User.find(params[:id]) #users whose posts, will be followed by another user
  value = true #will stay true unless changed
  user_to_be_followed.posts.each do |this_post| 
    if current_user.follows?(this_board) != true
      value = false
    end 
  end
  value #returned
end
def number_of_posts_that_are_followed
  user_to_be_followed = User.find(params[:id]) #users whose posts, will be followed by another user
  value = true #will stay true unless changed
  user_to_be_followed.posts.each do |this_post| 
    if current_user.follows?(this_board) != true
      value = false
    end 
  end
  value #returned
end
胡大本事 2025-01-05 21:58:01

对 SimonMayer 的一点重构:

def number_of_posts_that_are_followed
  User.find(params[:id]).posts.each do |this_post| 
    return false unless current_user.follows?(this_post)
  end
  true
end

编辑:
甚至更短,红宝石风格:

def number_of_posts_that_are_followed
  User.find(params[:id]).posts.map do |this_post| 
    not current_user.follows?(this_post)
  end.any?
end

A little refactoring of SimonMayer's :

def number_of_posts_that_are_followed
  User.find(params[:id]).posts.each do |this_post| 
    return false unless current_user.follows?(this_post)
  end
  true
end

EDIT:
Even shorter, ruby-style :

def number_of_posts_that_are_followed
  User.find(params[:id]).posts.map do |this_post| 
    not current_user.follows?(this_post)
  end.any?
end
記憶穿過時間隧道 2025-01-05 21:58:01
def number_of_posts_that_are_followed
  user_to_be_followed = User.find(params[:id]) #users whose posts, will be followed by another user
  user_to_be_followed.posts.each do |this_post| 
    if current_user.follows?(this_board) != true
      return false
    end 
  end
  return true
end
def number_of_posts_that_are_followed
  user_to_be_followed = User.find(params[:id]) #users whose posts, will be followed by another user
  user_to_be_followed.posts.each do |this_post| 
    if current_user.follows?(this_board) != true
      return false
    end 
  end
  return true
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文