summaryrefslogtreecommitdiff
path: root/spec/ruby/language/it_parameter_spec.rb
blob: 72023180d91d54f49d830f3ddd89b683305422ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require_relative '../spec_helper'

ruby_version_is "3.4" do
  describe "The `it` parameter" do
    it "provides it in a block" do
      -> { it }.call("a").should == "a"
      proc { it }.call("a").should == "a"
      lambda { it }.call("a").should == "a"
      ["a"].map { it }.should == ["a"]
    end

    it "assigns nil to not passed parameters" do
      proc { it }.call().should == nil
    end

    it "can be used in both outer and nested blocks at the same time" do
      -> { it + -> { it * it }.call(2) }.call(3).should == 7
    end

    it "is a regular local variable if there is already a 'it' local variable" do
        it = 0
        proc { it }.call("a").should == 0
    end

    it "raises SyntaxError when block parameters are specified explicitly" do
      -> { eval("-> () { it }")         }.should raise_error(SyntaxError, /ordinary parameter is defined/)
      -> { eval("-> (x) { it }")        }.should raise_error(SyntaxError, /ordinary parameter is defined/)

      -> { eval("proc { || it }")       }.should raise_error(SyntaxError, /ordinary parameter is defined/)
      -> { eval("proc { |x| it }")      }.should raise_error(SyntaxError, /ordinary parameter is defined/)

      -> { eval("lambda { || it }")     }.should raise_error(SyntaxError, /ordinary parameter is defined/)
      -> { eval("lambda { |x| it }")    }.should raise_error(SyntaxError, /ordinary parameter is defined/)

      -> { eval("['a'].map { || it }")  }.should raise_error(SyntaxError, /ordinary parameter is defined/)
      -> { eval("['a'].map { |x| it }") }.should raise_error(SyntaxError, /ordinary parameter is defined/)
    end

    it "affects block arity" do
      -> {}.arity.should == 0
      -> { it }.arity.should == 1
    end

    it "affects block parameters" do
      -> { it }.parameters.should == [[:req]]

      ruby_version_is ""..."4.0" do
        proc { it }.parameters.should == [[:opt, nil]]
      end
      ruby_version_is "4.0" do
        proc { it }.parameters.should == [[:opt]]
      end
    end

    it "does not affect binding local variables" do
      -> { it; binding.local_variables }.call("a").should == []
    end

    it "does not work in methods" do
      obj = Object.new
      def obj.foo; it; end

      -> { obj.foo("a") }.should raise_error(ArgumentError, /wrong number of arguments/)
    end
  end
end