return inside a code block

R

rtilley

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
 
O

OliverMarchand

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
 
C

ChrisH

rtilley said:
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
 
R

rtilley

OliverMarchand said:
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.
 
D

Daniel Schierbeck

OliverMarchand said:
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
 
R

rtilley

rtilley said:
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?
 
R

Robert Klemme

OliverMarchand said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,206
Messages
2,571,068
Members
47,674
Latest member
scazeho

Latest Threads

Top