You can’t define a module using module Auth()
:
irb(main):001:0> module Auth()
irb(main):002:1> end
SyntaxError: (irb):1: syntax error, unexpected '\n', expecting :: or '[' or '.'
and
irb(main):001:0> module Auth()
irb(main):002:1> def login(id)
irb(main):003:2> end
irb(main):004:1> end
SyntaxError: (irb):1: syntax error, unexpected '\n', expecting :: or '[' or '.'
(irb):4: syntax error, unexpected keyword_end, expecting end-of-input
Removing the trailing parenthesis fixes the error:
irb(main):001:0> module Auth
irb(main):002:1> def login(id)
irb(main):003:2> end
irb(main):004:1> end
=> :login
and
irb(main):001:0> module Auth
irb(main):002:1> module_function()
irb(main):003:1>
irb(main):004:1* def login(id)
irb(main):005:2> members = ['abc', 'def', 'ghi']
irb(main):006:2>
irb(main):007:2* for member in members do
irb(main):008:3* if (id == member)
irb(main):009:4> return true
irb(main):010:4> end
irb(main):011:3> end
irb(main):012:2> return false
irb(main):013:2> end
irb(main):014:1> end
=> :login
Your code could be more Ruby-like:
def login(id)
['abc', 'def', 'ghi'].each do |member|
return true if (id == member)
end
return false
end
login('ghi') # => true
login('foo') # => false
which can be refactored to:
def login(id)
['abc', 'def', 'ghi'].any? { |member| (id == member) }
end
login('ghi') # => true
login('foo') # => false
then to:
def login(id)
['abc', 'def', 'ghi'].include?(id)
end
login('ghi') # => true
login('foo') # => false
1
solved Syntax Error in Ruby. unexpected keyword_end, expecting end-of-inputs [closed]