0

I just began to learn ruby on rails. I'm doing a tutorial at the moment, but I'm also trying to understand the code that one of my collegue did. I have looked for the answers on internet but did not find any response which was answering my questions. So here are my questions :

On this code :

class Post
  require 'date'
  include Comparable
  attr_accessor :post_number, :author, :date

  def initialize

  end

  def <=> (another_post)
    self.date <=> another_post.date
  end
end

What does the method "<=>" means? Is it a copy of :post_number, :author and a change of :date? It seems that this method is not working anyway, but I'm just wondering the meaning of this method.

Second question : I found this code : a, b and c have proper value.

a = 0.1 * b if c?

Is this any different of :

if c?
  a = 0.1 * b
end

Well, thank you for your answers and I hope that my questions are not too stupid. Regards, Bdloul

Bdloul
  • 882
  • 10
  • 27
  • 1
    For your first question, see [What is the Ruby Spaceship operator](http://stackoverflow.com/questions/827649/what-is-the-ruby-spaceship-operator) For your second question, yes they are equivalent. – Michael Berkowski Jan 10 '13 at 15:03
  • 1
    Next time ask questions separately and try to give them meaningful titles -- it allows to build up knowledge base that can be later easily browsed by other users. – samuil Jan 10 '13 at 15:16

2 Answers2

1

<=> is the comparator method. If you did something like Post.all.sort, the sort method will look to see if Post defines a way to compare post objects. Otherwise, it will move up the inheritance hierarchy until it finds a way to sort them.

The <=> looks weird, but it's just a combination of < (is one object less than another object), = (are two objects equal), and > (is one object greater than the other).

So in your posted code, the author is making a method that will define how to compare two objects. In particular, he wrote that the way they will be compared is based on the way the .date attribute is compared. So in other words, a Post starting on January 1, 2013 will come before a post starting January 2, 2013, and so on.

As for the next part, the two pieces of code are identical. In Ruby, you can write many conditions on a single line. These are also all equivalent, just to give more examples:

# 1.
a = 1 unless a == 0
# or
unless a == 0
  a = 1
end

# 2.
a += 1 while a < 10
# or
while a < 10
  a += 1
end
MrDanA
  • 11,489
  • 2
  • 36
  • 47
  • Your example of modificator notation is wrong -- using extended version behaves differently when `a` variable is not defined. – samuil Jan 10 '13 at 15:18
  • Sorry I didn't mean to imply that a was already defined. I was not concerned with the code around the examples, just showing examples of the conditions themselves. – MrDanA Jan 10 '13 at 15:21
  • My point is that modifier notation is very convenient, but it is not identical to extended notation. – samuil Jan 10 '13 at 15:25
  • 1
    In the question, it is clearly stated that "a, b and c have proper value." – claasz Jan 10 '13 at 15:43
  • @Bdloul you're welcome! Remember to mark it as the answer to your question! – MrDanA Jan 11 '13 at 13:48
-1

<=> is a comparison operator. See What is the Ruby <=> (spaceship) operator?

The if statements are equivalent.

Community
  • 1
  • 1
claasz
  • 2,059
  • 1
  • 14
  • 16