Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions core/src/main/java/org/jruby/parser/RubyParserBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1791,8 +1791,14 @@ public static IDType id_type(ByteList identifier) {
int length = identifier.length();
byte last = (byte) identifier.get(length - 1);
if (last == '=') {
if (length > 1 && identifier.charAt(length - 2) == '=') {
return Local;
if (length > 1) {
char secondLast = identifier.charAt(length - 2);
// Comparison operators (==, ===, !=, <=, >=) end in '=' but
// are not setter (attrset) names. Genuine setters (foo=, []=)
// never have '=', '!', '<' or '>' as their second to last char.
if (secondLast == '=' || secondLast == '!' || secondLast == '<' || secondLast == '>') {
return Local;
}
}
return AttrSet;
}
Expand Down
28 changes: 28 additions & 0 deletions spec/ruby/language/method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,34 @@ def m(...) = mm(...) + mm(...)
m("meow", num: 2).should == "meow" * 4
end
end

context "with an operator name ending in `=`" do
it "defines the operator methods" do
eval <<-ruby
class OpEndlessOperator
def !=(other) = "not-equal"
def <=(other) = "less-or-equal"
def >=(other) = "greater-or-equal"
end
ruby

obj = OpEndlessOperator.new
obj.send(:!=, 1).should == "not-equal"
obj.send(:<=, 1).should == "less-or-equal"
obj.send(:>=, 1).should == "greater-or-equal"
end
end

context "with a setter name" do
it "raises a SyntaxError" do
-> {
eval("class SetterEndless; def foo=(x) = x; end")
}.should.raise(SyntaxError)
-> {
eval("class SetterEndless; def []=(k, v) = v; end")
}.should.raise(SyntaxError)
end
end
end

describe "Keyword arguments are now separated from positional arguments" do
Expand Down
Loading