Skip to content

Latest commit

 

History

History
138 lines (98 loc) · 2.09 KB

Ruby.md

File metadata and controls

138 lines (98 loc) · 2.09 KB

Table of Contents

Ruby

Interpreter


Parenthesis matters

> def test(x); x*2; end
= > :test

> test 1
=> 2
> test(1)
=> 2
> test(1)+1
=> 3
> test (1)+1
=> 4

In some cases, the whitespaces and parenthesis are interpreter differently, resulting in priority change in the evaluation of the expression.

Tested with: irb 2.3


End-of-line matters

def test1 (x,y)
  x +
  y
end

def test2 (x,y)
 x
 + y
end

test1(1,2)
test2(1,2)

Expected result: 3, 3

Result: 3, 2

Reason: in test1, the + operator is before an EOL, so the interpreter assumes the line is continued after. In test2, the + y is interpreter as the unary + operator applied to y, to x is ignored and this function always returns y.

Tested with: irb 2.3


Poisoning


The many ways to compare an integer

> 3==4
=> false
> 3.eql?4
=> false
> 3.equal?4
=> false

> class Fixnum; def ==(y); true; end; end
=> nil
> 3==4
=> true
> 3.eql?4
=> true
> 3.equal?4
=> false

There are many ways to compare a value. When overridind the == operator for a class, the comparisons are changed, but not all of them.

Tested with: irb 2.3


Unexpected and surprising


Methods vs local variables

class MyClass
  def value
    "my_value"
  end

  def do_things
    puts value
    if false
      value = nil
    end
    puts value
  end
end

MyClass.new.do_things

Expected result: my_value and my_value

Result: my_value and

Reason: first occurence of value is a method call, and even though value = nil is not executed, the parser see value as a local variable after the if block.

Tested with: ruby 2.3.3