On Tuesday 06 September 2011 18:11:47 Philipp Altmann wrote:
> Hi guys,
> 
> this is my Code:
> 
>   def getRegex(string)
>     if @HashMapWithAllRegex.has_key?(string) ?
> (@HashMapWithAllRegex.fetch(string) : raise(ArgumentError, "This regex
> is not specified")
>     end
>   end
> 
> Error Message:
> 
> SyntaxError:
> /Users/Philipp/Documents/projects/objcdoc/src/RegexProvider.rb:19:
> syntax error, unexpected tIVAR
> 
>     if @HashMapWithAllRegex.has_key?(string) ? return
> @HashMapWithAllRegex.fetch(string : raise(ArgumentError, "This regex is
> not specified")
>                                                                          ^
>   require at org/jruby/RubyKernel.java:1038
>    (root) at
> /Users/Philipp/Documents/projects/objcdoc/src/RegexProvider.rb:6
>   require at org/jruby/RubyKernel.java:1038
>    (root) at /Users/Philipp/Documents/projects/objcdoc/src/Main.rb:1
> 
> 
> Why will this don't work? I think in Java will this construct work :-).

Well, ruby is not java. At any rate, why do you use the ternary operator 
together with an if? You only need one of the two:

def getRegexp(string)
  if @HashMapWithAllRegex.has_key?(string)
    @HashMapWithAllRegex.fetch(string)
  else raise(ArgumentError, "This regex is not specified")
  end
end

or 

def getRegexp(string)
  # The following should be on one line
  @HashMapWithAllRegex.has_key?(string) ? @HashMapWithAllRegex.fetch(string) : 
raise(ArgumentError, "This regex is not specified")
end

I'd very much prefer the former. Better yet (supposing that 
@HashMapWithAllRegex is a hash), you can do:

def getRegexp(string)
  @HashMapWithAllRegex.fetch(string) do |k|
    raise(ArgumentError, "This regex is not specified")
  end
end

I hope this helps

Stefano