[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

return inside a code block

Brad Tilley

4/12/2006 2:50:00 PM

Is it proper to return something while in a code block like this:

def test_get_os
# Get the OS version.
wmi = WIN32OLE.connect("winmgmts:\\\\.")
wmi.InstancesOf("win32_operatingsystem").each do |o|
os = o.caption
return os
end
end

Or, is it more appropriate to return outside of the block like this:

def test_get_os
# Get the OS version.
os = ''
wmi = WIN32OLE.connect("winmgmts:\\\\.")
wmi.InstancesOf("win32_operatingsystem").each do |o|
os = o.caption
end
return os
end

Maybe I'm being too pedantic, I'm just curious as to which way is better.

Thanks,
Brad
6 Answers

OliverMarchand

4/12/2006 3:03:00 PM

0

I am guessing your code does not what you want it to do:

(a) In the inside version you are returning the first element, which
you could do with [0], instead of each

(b) in the second version you are rturning the last one, successively
overwriting os. Use [-1] if that is waht you intend.

Am I guessing right that this is not what you would like?

Back to your original question: I am not an expert, but I would guess
it is clearer to return from the outside, because the return inside a
code block could be confused with the result passed back to the
iterator. --- I am also awaiting other anwsers

cheers,
Oliver

Chris Hulan

4/12/2006 3:12:00 PM

0

rtilley wrote:
> Is it proper to return something while in a code block like this:
>
> def test_get_os
> # Get the OS version.
> wmi = WIN32OLE.connect("winmgmts:\\\\.")
> wmi.InstancesOf("win32_operatingsystem").each do |o|
> os = o.caption
> return os
> end
> end

Using each implies you expect >1 items, but using return inside the
block will only return the first

>
> Or, is it more appropriate to return outside of the block like this:
>
> def test_get_os
> # Get the OS version.
> os = ''
> wmi = WIN32OLE.connect("winmgmts:\\\\.")
> wmi.InstancesOf("win32_operatingsystem").each do |o|
> os = o.caption
> end
> return os
> end

Moving the return outside the block will, if there are >1 items, only
return the last

Given there is only 1 OS version I would not use a block, if thats
possible. Otherwise maybe a comment to clarify, and then which ever
version floats your boat

Cheers

Brad Tilley

4/12/2006 3:15:00 PM

0

OliverMarchand wrote:
> I am guessing your code does not what you want it to do:

No, it works fine... run that code on a Windows XP or 2003 computer and
see what you get. It will tell you the name of your operating system
whether you return in the code block or outside of it.

Perhaps I should not do InstancesOf? But would there be more than one
instance of a running OS through WMI?

> (a) In the inside version you are returning the first element, which
> you could do with [0], instead of each
>
> (b) in the second version you are rturning the last one, successively
> overwriting os. Use [-1] if that is waht you intend.
>
> Am I guessing right that this is not what you would like?

I assuming there is only one instance. I may be wrong.

Daniel Schierbeck

4/12/2006 3:19:00 PM

0

OliverMarchand wrote:
> I am guessing your code does not what you want it to do:
>
> (a) In the inside version you are returning the first element, which
> you could do with [0], instead of each
>
> (b) in the second version you are rturning the last one, successively
> overwriting os. Use [-1] if that is waht you intend.
>
> Am I guessing right that this is not what you would like?
>
> Back to your original question: I am not an expert, but I would guess
> it is clearer to return from the outside, because the return inside a
> code block could be confused with the result passed back to the
> iterator. --- I am also awaiting other anwsers

I can't find the post you replied to, but I guess the author of it asked
how to manually stop a block from being executed, and return a value?

(1...5).each { |num| break num if num > 2 } => 3

Cheers,
Daniel

Brad Tilley

4/12/2006 3:43:00 PM

0

rtilley wrote:

> I assuming there is only one instance. I may be wrong.

require 'win32ole'

class TESTER

def test_get_os
count = 0
wmi = WIN32OLE.connect("winmgmts:\\\\.")
wmi.InstancesOf("win32_operatingsystem").each do |o|
os = o.caption
count = count + 1
puts os
puts count
end
end

end

x = TESTER.new
x. test_get_os

I only see one instance. count only increments once. So, in this case, I
don't suppose it matters whether I return inside or outside the block,
right?

Robert Klemme

4/12/2006 4:05:00 PM

0

OliverMarchand wrote:
> I am guessing your code does not what you want it to do:
>
> (a) In the inside version you are returning the first element, which
> you could do with [0], instead of each
>
> (b) in the second version you are rturning the last one, successively
> overwriting os. Use [-1] if that is waht you intend.
>
> Am I guessing right that this is not what you would like?

The code probably does what the OP wanted it to do but the two code
snippets are by far not equivalent as you correctly detected.

> Back to your original question: I am not an expert, but I would guess
> it is clearer to return from the outside, because the return inside a
> code block could be confused with the result passed back to the
> iterator. --- I am also awaiting other anwsers

I prefer returning from inside a block or wherever because it makes code
more readable IMHO (control constructs become easier). Other folks
mandate to have only a single return per method / function but I
personally think this is nonsense.

Kind regards

robert