From: Amir Ebrahimifard on
Hi
What is happen for return value in these 2 codes ?
(Please analysis for me result of these 2 codes)

----------------------------

def test1(name)
puts "Hello"+name
end

def test2(name)
puts "Hello"+name
1+1
end

return_value1 = test1("amir")
puts return_value1

return_value2 = test2("amir")
puts return_value2

----------------------------
--
Posted via http://www.ruby-forum.com/.

From: Alex Stahl on
If you don't explicitly return anything, ruby returns the value of the
last expression.

On Wed, 2010-08-04 at 16:40 -0500, Amir Ebrahimifard wrote:
> Hi
> What is happen for return value in these 2 codes ?
> (Please analysis for me result of these 2 codes)
>
> ----------------------------
>
> def test1(name)
> puts "Hello"+name
> end
>
> def test2(name)
> puts "Hello"+name
> 1+1
> end
>
> return_value1 = test1("amir")
> puts return_value1
>
> return_value2 = test2("amir")
> puts return_value2
>
> ----------------------------



From: James on
[Note: parts of this message were removed to make it a legal post.]

On Wed, Aug 4, 2010 at 3:52 PM, Alex Stahl <astahl(a)hi5.com> wrote:

> If you don't explicitly return anything, ruby returns the value of the
> last expression.
>
> On Wed, 2010-08-04 at 16:40 -0500, Amir Ebrahimifard wrote:
> > Hi
> > What is happen for return value in these 2 codes ?
> > (Please analysis for me result of these 2 codes)
>
> Sounds vaguely like a homework problem, dude.

From: brabuhr on
On Wed, Aug 4, 2010 at 5:40 PM, Amir Ebrahimifard <amiref(a)ymail.com> wrote:
> What is happen for return value in these 2 codes ?
> (Please analysis for me result of these 2 codes)

$ cat i.rb
def test1(name)
puts "Hello"+name
end

def test2(name)
puts "Hello"+name
1+1
end

return_value1 = test1("amir")
p return_value1

return_value2 = test2("amir")
p return_value2

$ ruby i.rb
Helloamir
nil
Helloamir
2

 | 
Pages: 1
Prev: Default arguments problem
Next: Send Text w/ Ruby