Exiting of a block

Hello,

it's a newbie question...
i wanted to do something like:

result1 = [1, 2, 3].find { |e|
if e == 2
return true
else
return false
end
}
(with several if/else one in each other rather than one big condition, for
code readability)

but I realised that ‘return’ does not apply to a block, but only to a
function. it’s a bit (actually very) un-intuitive for me, but anyway, is
there a nicer solution than doing my current:

item = [1, 2, 3].find { |e|
result = false
if e == 2
result = true
else
result = false
end
result
}

for reference, here is my real code. I’m searching the current TV program at
showTime from a DB of tv programs i fetched from the net. a program for sure
has a start time (time), but not necessarily an end time (if it’s the last tv
program on the web page, i can’t know).

# now find the current program for this
# channel.
programsForChannel.find { |program|
	result = false
	if program.time <= showTime
		if program.endTime
			# there is a endtime.
			# are we now before this end?
			if program.endTime > showTime
				result = true
			end
		else
			# program has no registered
			# end.
			result = true
		end
	end
	result
}

any help appreciated!

emmanuel

···


“If there is any kind of God, it’s not in you or in me,
but in the space between us”
– Celine, “Before Sunrise”
(It’s not about what you do, it’s about what you give.)

Hi,

result1 = [1, 2, 3].find { |e|
if e == 2
return true
else
return false
end
}
(with several if/else one in each other rather than one big condition, for
code readability)

but I realised that ‘return’ does not apply to a block, but only to a
function. it’s a bit (actually very) un-intuitive for me, but anyway, is
there a nicer solution than doing my current:

If you switch to 1.8.0, there’s nicer way:

result = [1, 2, 3].find { |e|
if e == 2
break true
else
break false
end
}

						matz.
···

In message “exiting of a block” on 03/05/26, Emmanuel Touzery emmanuel.touzery@wanadoo.fr writes:

“Emmanuel Touzery” emmanuel.touzery@wanadoo.fr schrieb im Newsbeitrag
news:200305260838.02882.emmanuel.touzery@wanadoo.fr

Hello,

it’s a newbie question…
i wanted to do something like:

result1 = [1, 2, 3].find { |e|
if e == 2
return true
else
return false
end
}
(with several if/else one in each other rather than one big condition,
for
code readability)

but I realised that ‘return’ does not apply to a block, but only to a
function. it’s a bit (actually very) un-intuitive for me, but anyway, is
there a nicer solution than doing my current:

item = [1, 2, 3].find { |e|
result = false
if e == 2
result = true
else
result = false
end
result
}

for reference, here is my real code. I’m searching the current TV
program at
showTime from a DB of tv programs i fetched from the net. a program for
sure
has a start time (time), but not necessarily an end time (if it’s the
last tv
program on the web page, i can’t know).

now find the current program for this

channel.

programsForChannel.find { |program|
result = false
if program.time <= showTime
if program.endTime

there is a endtime.

are we now before this end?

if program.endTime > showTime
result = true
end
else

program has no registered

end.

result = true
end
end
result
}

any help appreciated!

In your case you can use “return” even with older versions of ruby if you
use a method:

def find
[1, 2, 3].find { |e|
return true if e == 2
}

false
end

Are you aware of the standard methods Enumerable#include?,
Enumerable#find, Enumerable#detect, Enumerable#select, Enumerable#find_all
and Enumerable#grep? These should provide the functionality you need.

Regards

robert

Hi,

···

At Mon, 26 May 2003 15:30:28 +0900, Emmanuel Touzery wrote:

now find the current program for this

channel.

programsForChannel.find { |program|
result = false
if program.time <= showTime
if program.endTime
# there is a endtime.
# are we now before this end?
if program.endTime > showTime
result = true
end
else
# program has no registered
# end.
result = true
end
end
result
}

In this case, you can:

programsForChannel.find { |program|
	program.time <= showTime && (!program.endTime || program.endTime > showTime)
}


Nobu Nakada

it’s a newbie question…
i wanted to do something like:

result1 = [1, 2, 3].find { |e|
if e == 2
return true
else
return false
end
}
[…]

A possibility:

result1 = [1,2,3].find { |e|
catch (:res) {
if e == 2
throw :res, true
else
throw :res, false
end
}
}

Emmanuel Touzery emmanuel.touzery@wanadoo.fr wrote in message news:200305260838.02882.emmanuel.touzery@wanadoo.fr

Hello,

it’s a newbie question…
i wanted to do something like:

result1 = [1, 2, 3].find { |e|
if e == 2
return true
else
return false
end
}
(with several if/else one in each other rather than one big condition, for
code readability)

Can’t you just do:

[1, 2, 3].find{|e|
if e == 2
true
else
false
end
}

? You can nest several if-else-blocks inside if you want, with each
block returning the last statement…

I think I understood your problem, but ignore me if this is not what
you’re seeking :slight_smile:

“Tore Darell” tored@stud.ntnu.no schrieb im Newsbeitrag
news:7bffddce.0305301538.49737a54@posting.google.com

Emmanuel Touzery emmanuel.touzery@wanadoo.fr wrote in message
news:200305260838.02882.emmanuel.touzery@wanadoo.fr

Hello,

it’s a newbie question…
i wanted to do something like:

result1 = [1, 2, 3].find { |e|
if e == 2
return true
else
return false
end
}
(with several if/else one in each other rather than one big condition,
for
code readability)

Can’t you just do:

[1, 2, 3].find{|e|
if e == 2
true
else
false
end
}

If you’re proposoing a simplification then why not directly

[1, 2, 3].find {|e| e == 2 }

robert