What Is the "Equals Greater Than" Operator => in Ruby

What is the equals greater than operator = in Ruby?

It is mainly a ruby operator that sets the value of a key inside a hash. Thus :

{ :minimum => 5 }

Is a ruby hash that has the symbol :minimum as a key that maps to the value of 5. A hash with one entry, in this example. Same for :

:presence => true

Still a hash. However, in ruby, when you have a method, you can omit the {} that surround a hash. That is what happens with the validates method. It's a method and thus the passed hash does not explicitly need {}.

In Ruby what does = mean and how does it work?

=> separates the keys from the values in a hashmap literal. It is not overloadable and not specifically connected to symbols.

A hashmap literal has the form {key1 => value1, key2 => value2, ...}, but when used as the last parameter of a function, you can leave off the curly braces. So when you see a function call like f(:a => 1, :b => 2), f is called with one argument, which is a hashmap that has the keys :a and :b and the values 1 and 2.

What does ||= (or-equals) mean in Ruby?

This question has been discussed so often on the Ruby mailing-lists and Ruby blogs that there are now even threads on the Ruby mailing-list whose only purpose is to collect links to all the other threads on the Ruby mailing-list that discuss this issue.

Here's one: The definitive list of ||= (OR Equal) threads and pages

If you really want to know what is going on, take a look at Section 11.4.2.3 "Abbreviated assignments" of the Ruby Language Draft Specification.

As a first approximation,

a ||= b

is equivalent to

a || a = b

and not equivalent to

a = a || b

However, that is only a first approximation, especially if a is undefined. The semantics also differ depending on whether it is a simple variable assignment, a method assignment or an indexing assignment:

a    ||= b
a.c ||= b
a[c] ||= b

are all treated differently.

What is the - (dash greater than) operator in Ruby/Rails

This is syntactic sugar.

->(external_id) { where(external_id: external_id) }

is equal to:

lambda { |external_id| where(external_id: external_id) }

Ruby = operator

The symbol "=>" is not an operator. It's just a syntactic means to express that there is a relationship of "key-value" between the other two elements. It's used to define hashes (or associative arrays, as they're called in some other languages, eg. PHP). In this sense, because "=>" it's not an operator, it doesn't do anything (so as symbols "[" and "]" don't do anything when used to define an array). If you are still confused, have a look into the Hash Ruby class and compare it to the Array class.

What's the difference between equal?, eql?, ===, and ==?

I'm going to heavily quote the Object documentation here, because I think it has some great explanations. I encourage you to read it, and also the documentation for these methods as they're overridden in other classes, like String.

Side note: if you want to try these out for yourself on different objects, use something like this:

class Object
def all_equals(o)
ops = [:==, :===, :eql?, :equal?]
Hash[ops.map(&:to_s).zip(ops.map {|s| send(s, o) })]
end
end

"a".all_equals "a" # => {"=="=>true, "==="=>true, "eql?"=>true, "equal?"=>false}


== — generic "equality"

At the Object level, == returns true only if obj and other are the same object. Typically, this method is overridden in descendant classes to provide class-specific meaning.

This is the most common comparison, and thus the most fundamental place where you (as the author of a class) get to decide if two objects are "equal" or not.

=== — case equality

For class Object, effectively the same as calling #==, but typically overridden by descendants to provide meaningful semantics in case statements.

This is incredibly useful. Examples of things which have interesting === implementations:

  • Range
  • Regex
  • Proc (in Ruby 1.9)

So you can do things like:

case some_object
when /a regex/
# The regex matches
when 2..4
# some_object is in the range 2..4
when lambda {|x| some_crazy_custom_predicate }
# the lambda returned true
end

See my answer here for a neat example of how case+Regex can make code a lot cleaner. And of course, by providing your own === implementation, you can get custom case semantics.

eql?Hash equality

The eql? method returns true if obj and other refer to the same hash key. This is used by Hash to test members for equality. For objects of class Object, eql? is synonymous with ==. Subclasses normally continue this tradition by aliasing eql? to their overridden == method, but there are exceptions. Numeric types, for example, perform type conversion across ==, but not across eql?, so:

1 == 1.0     #=> true
1.eql? 1.0 #=> false

So you're free to override this for your own uses, or you can override == and use alias :eql? :== so the two methods behave the same way.

equal? — identity comparison

Unlike ==, the equal? method should never be overridden by subclasses: it is used to determine object identity (that is, a.equal?(b) iff a is the same object as b).

This is effectively pointer comparison.

Rails: Using greater than/less than with a where statement

Try this

User.where("id > ?", 200) 

Meaning of tilde-greater-than (~ ) in version requirement?

The RubyGems manual calls this a
pessimistic version constraint.

Assume you have specified an n-part version number, e.g. 1.3 (2-part) or
3.5.6.2 (4-part) as the constraint. Then, in order to fulfill the constraint,
a version number must satisfy both of the following conditions

  1. The first n-1 parts of the version number must be identical to the first n-1
    parts of the constraint
    (e.g. 1.x or 3.5.6.x match, but 0.x or 3.5.7.x don't) and

  2. The last part of the version number must be greater than or equal to the last
    part of the constraint
    (e.g. 1.9999 and 3.5.6.2 match, but 1.2 or 3.5.6.1 don't).

In other words


~> x1.x2.x3. … .xn-2.xn-1.xn

matches


x1.x2.x3. … .xn-2.xn-1.y, y >= xn

The reason this is called a "pessimistic" constraint, and also the use case for
it, is that when you just say > x.y.z, you are being optimistic: you assume
that from here on out, until all eternity, the API will never ever change. This
is of course a pretty bold assumption. However, most projects have rules about
when they are allowed to
break backwards compatibility,
and how they have to change their version number when they do break backwards
compatibility. You can encode those version numbering rules using a pessimistic
constraint, and thus you can be sure that your code will always continue to work
(assuming that the author of the other project actually adheres to his own
rules, which unfortunately isn't always the case).

What does tilde-greater-than (~ ) mean in Ruby gem dependencies?

It means "equal to or greater than in the last digit", so e.g. ~> 2.3 means
"equal to 2.3 or greater than 2.3, but less than 3.0", while ~> 2.3.0 would
mean "equal to 2.3.0 or greater than 2.3.0, but less than 2.4.0".

You can pronounce it as "approximately greater than".

§ Pessimistic version constraint

Difference between or and || in Ruby?

It's a matter of operator precedence.

|| has a higher precedence than or.

So, in between the two you have other operators including ternary (? :) and assignment (=) so which one you choose can affect the outcome of statements.

Here's a ruby operator precedence table.

See this question for another example using and/&&.

Also, be aware of some nasty things that could happen:

a = false || true  #=> true
a #=> true

a = false or true #=> true
a #=> false

Both of the previous two statements evaluate to true, but the second sets a to false since = precedence is lower than || but higher than or.



Related Topics



Leave a reply



Submit