On Nov 22, 1:47 am, Martin Durai <mar... / angleritech.com> wrote:
> hi gavin
>
>  Iam in a task of porting java class into ruby. so how to port the
> following java code into ruby. I didnt understood exactly what java does
> with the following code. so could you help me to port this one.
>
>                 public String getNamespace()
>           {
>         if(eventType == START_TAG) {
>             return processNamespaces ? elUri[ depth  ] : NO_NAMESPACE;
>         } else if(eventType == END_TAG) {
>             return processNamespaces ? elUri[ depth ] : NO_NAMESPACE;
>         }
>         return null;
>        }

It seems that you don't know what the Java code is doing, and you
don't know how to program in Ruby. I would say you have a lot of work
ahead of you.

Are eventType and processNamespaces and elUri and depth all global
variables? Let's assume so.

The following is valid Ruby code, if you have defined the appropriate
constants:

  def getNamespace
    if $eventType == START_TAG
      return $processNamespaces ? $elUri[ $depth ] : NO_NAMESPACE
    elsif $eventType == END_TAG
      return $processNamespaces ? $elUri[ $depth ] : NO_NAMESPACE
    else
      return nil
    end
  end

If you don't understand the "a ? b : c" syntax, google for "ternary
operator".

Here is the same code as the above written in increasingly simpler
ways:

  def getNamespace
    if $eventType == START_TAG || $eventType == END_TAG
      return $processNamespaces ? $elUri[ $depth ] : NO_NAMESPACE
    else
      return nil
    end
  end

  def getNamespace
    if $eventType == START_TAG || $eventType == END_TAG
      $processNamespaces ? $elUri[ $depth ] : NO_NAMESPACE
    else
      nil
    end
  end

  def getNamespace
    if $eventType == START_TAG || $eventType == END_TAG
      $processNamespaces ? $elUri[ $depth ] : NO_NAMESPACE
    end
  end

  def getNamespace
    case $eventType
      when START_TAG, END_TAG
        $processNamespaces ? $elUri[ $depth ] : NO_NAMESPACE
    end
  end

  def getNamespace
    if [ START_TAG, END_TAG ].include?( $eventType )
      $processNamespaces ? $elUri[ $depth ] : NO_NAMESPACE
    end
  end