Project

General

Profile

Feature #14951 ยป teq.patch

prototype implementation - danga (Dan Garubba), 07/30/2018 01:35 AM

View differences:

defs/id.def
95 95
  GE            >=      GEQ
96 96
  Eq            ==      EQ
97 97
  Eqq           ===     EQQ
98
  Teq           =?      TEQ
98 99
  Neq           !=      NEQ
99 100
  Not           !
100 101
  Backquote     `
object.c
139 139
}
140 140

  
141 141
/**
142
 *  call-seq:
143
 *     obj =? other   -> true or false
144
 *
145
 *  Test Equality -- Returns true if \a obj and \a other are both
146
 *  either truthy or falsy and false otherwise.
147
 */
148

  
149
VALUE
150
rb_teq(VALUE obj1, VALUE obj2)
151
{
152
    return (RTEST(obj1) == RTEST(obj2)) ? Qtrue : Qfalse;
153
}
154

  
155
/**
142 156
 * Determines if \a obj1 and \a obj2 are equal in terms of
143 157
 * \c Object#eql?.
144 158
 *
......
4060 4074

  
4061 4075
    rb_define_method(rb_mKernel, "nil?", rb_false, 0);
4062 4076
    rb_define_method(rb_mKernel, "===", rb_equal, 1);
4077
    rb_define_method(rb_mKernel, "=?", rb_teq, 1);
4063 4078
    rb_define_method(rb_mKernel, "=~", rb_obj_match, 1);
4064 4079
    rb_define_method(rb_mKernel, "!~", rb_obj_not_match, 1);
4065 4080
    rb_define_method(rb_mKernel, "eql?", rb_obj_equal, 1);
parse.y
860 860
%token tCMP		RUBY_TOKEN(CMP)    "<=>"
861 861
%token tEQ		RUBY_TOKEN(EQ)     "=="
862 862
%token tEQQ		RUBY_TOKEN(EQQ)    "==="
863
%token tTEQ		RUBY_TOKEN(TEQ)    "=?"
863 864
%token tNEQ		RUBY_TOKEN(NEQ)    "!="
864 865
%token tGEQ		RUBY_TOKEN(GEQ)    ">="
865 866
%token tLEQ		RUBY_TOKEN(LEQ)    "<="
......
908 909
%nonassoc tDOT2 tDOT3
909 910
%left  tOROP
910 911
%left  tANDOP
911
%nonassoc  tCMP tEQ tEQQ tNEQ tMATCH tNMATCH
912
%nonassoc  tCMP tEQ tEQQ tTEQ tNEQ tMATCH tNMATCH
912 913
%left  '>' tGEQ '<' tLEQ
913 914
%left  '|' '^'
914 915
%left  '&'
......
1786 1787
		| tCMP		{ ifndef_ripper($$ = tCMP); }
1787 1788
		| tEQ		{ ifndef_ripper($$ = tEQ); }
1788 1789
		| tEQQ		{ ifndef_ripper($$ = tEQQ); }
1790
		| tTEQ		{ ifndef_ripper($$ = tTEQ); }
1789 1791
		| tMATCH	{ ifndef_ripper($$ = tMATCH); }
1790 1792
		| tNMATCH	{ ifndef_ripper($$ = tNMATCH); }
1791 1793
		| '>'		{ ifndef_ripper($$ = '>'); }
......
1998 2000
		    {
1999 2001
			$$ = call_bin_op(p, $1, idEqq, $3, &@2, &@$);
2000 2002
		    }
2003
		| arg tTEQ arg
2004
		    {
2005
			$$ = call_bin_op(p, $1, idTeq, $3, &@2, &@$);
2006
		    }
2001 2007
		| arg tNEQ arg
2002 2008
		    {
2003 2009
			$$ = call_bin_op(p, $1, idNeq, $3, &@2, &@$);
......
7822 7828
	    pushback(p, c);
7823 7829
	    return tEQ;
7824 7830
	}
7825
	if (c == '~') {
7831
	if (c == '?') {
7832
	    return tTEQ;
7833
	}
7834
	else if (c == '~') {
7826 7835
	    return tMATCH;
7827 7836
	}
7828 7837
	else if (c == '>') {
test/ruby/test_object.rb
179 179
    assert_equal(false, false ^ nil)
180 180
  end
181 181

  
182
  def test_teq
183
    o = Object.new
184
    assert_equal(true, o =? Object.new)
185
    assert_equal(false, o =? false)
186
    assert_equal(false, o =? nil)
187
    assert_equal(true, false =? false)
188
    assert_equal(true, false =? nil)
189
    assert_equal(false, false =? o)
190
    assert_equal(true, nil =? false)
191
    assert_equal(true, nil =? nil)
192
    assert_equal(false, nil =? o)
193
  end
194

  
182 195
  def test_methods
183 196
    o = Object.new
184 197
    a1 = o.methods