# Return type

**URL:** <https://www.ruby-forum.com/t/return-type/120873>\
**Category:** Ruby\
**Created:** [November 21, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/return-type/120873 "2007-11-21T12:11:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 21, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/return-type/120873/1 "2007-11-21T12:11:01Z")

</div>

Actually the following code is a java return type. could any one help to  
do the same in ruby.

```
 return processNamespaces ? elUri[depth] : NO_NAMESPACE

```

Thank you in advance

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 21, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/return-type/120873/2 "2007-11-21T15:06:59Z")

</div>

On Nov 21, 4:11 am, Martin D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually the following code is a java return type. could any one help to  
> do the same in ruby.
> 
> ```
> return processNamespaces ? elUri[depth] : NO_NAMESPACE
> 
> ```

I don’t understand your question.

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 22, 2007, 9:47am UTC](https://www.ruby-forum.com/t/return-type/120873/3 "2007-11-22T09:47:54Z")

</div>

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;
   }

```

Gavin K. wrote:

> On Nov 21, 4:11 am, Martin D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Actually the following code is a java return type. could any one help to  
> > do the same in ruby.
> > 
> > ```
> > return processNamespaces ? elUri[depth] : NO_NAMESPACE
> > 
> > ```
> 
> I don’t understand your question.

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 23, 2007, 5:53am UTC](https://www.ruby-forum.com/t/return-type/120873/4 "2007-11-23T05:53:42Z")

</div>

Thank you Gavin your answers really helped me to code well

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [November 23, 2007, 5:15am UTC](https://www.ruby-forum.com/t/return-type/120873/5 "2007-11-23T05:15:38Z")

</div>

On Nov 22, 1:47 am, Martin D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> } 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

---

<div class="post-metadata">

**Author:** ![martin\_mercy2001](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d2c977/32.png) [@martin\_mercy2001](https://www.ruby-forum.com/u/martin_mercy2001)\
**Post date:** [April 16, 2008, 5:01pm UTC](https://www.ruby-forum.com/t/return-type/120873/6 "2008-04-16T17:01:17Z")

</div>

Gavin K. wrote:

> def getNamespace  
> if [START\_TAG, END\_TAG].include?( $eventType )  
> $processNamespaces ? $elUri[$depth] : NO\_NAMESPACE  
> end  
> end

And of course, the oneliner:

def getNamespace  
[START\_TAG, END\_TAG].include?( $eventType ) &&  
($processNamespaces ? $elUri[$depth] : NO\_NAMESPACE)  
end
