I must do a lot of verifications inside a def so i would like to know to
compact return and error notification
thanks
def self.doSomething(*args)
# Normal approach
if (0..4).include?index
puts "you has problem A"
return false
end
# Desired approach
return_with_info(false,"you has problem A") unless (0..4).include?index
end
I must do a lot of verifications inside a def so i would like to know to
compact return and error notification
thanks
Your ideal API of:
return_with_info(false,"you has problem A") unless (0..4).include?index
would take some rather dark magic to make happen, since you want to
have something return a value without a return statement when you're
not necessarily at the last line of a method.
HOWEVER!
You can do something like:
return barf(false, "you has problem A") unless (0..4).include?index
by defining a very simple method, such as:
def barf(val, msg)
puts msg
val
end
Now you're using "return" again, so it can be anywhere, and you can
give your method some better name appropriate to your situation. (And
since such outputting and value-setting is now centralized, you can do
all kinds of other things in there like logging, notifying, counting,
etc.)
-Dave
···
On Mon, Jan 1, 2018 at 8:42 PM, ng khanh <cnkhanh1986@gmail.com> wrote:
--
Dave Aronson, Software Development Consultant
T. Rex of Codosaurus, LLC (http://www.Codosaur.us)
What can we evolve for you today?
This should work for you
def return_with_info(value, message)
{value: value, message: message}
end
def self.doSomething(*args)
# Desired approach
return_with_info(false,"you has problem A") unless (0..4).include?index
end
···
On 2 January 2018 at 05:06, Dave Aronson <ruby-talk.list.2.TRex@codosaur.us> wrote:
On Mon, Jan 1, 2018 at 8:42 PM, ng khanh <cnkhanh1986@gmail.com> wrote:
> I must do a lot of verifications inside a def so i would like to know to
> compact return and error notification
> thanksYour ideal API of:
> return_with_info(false,"you has problem A") unless (0..4).include?index
would take some rather dark magic to make happen, since you want to
have something return a value without a return statement when you're
not necessarily at the last line of a method.HOWEVER!
You can do something like:
return barf(false, "you has problem A") unless (0..4).include?index
by defining a very simple method, such as:
def barf(val, msg)
puts msg
val
endNow you're using "return" again, so it can be anywhere, and you can
give your method some better name appropriate to your situation. (And
since such outputting and value-setting is now centralized, you can do
all kinds of other things in there like logging, notifying, counting,
etc.)-Dave
--
Dave Aronson, Software Development Consultant
T. Rex of Codosaurus, LLC (http://www.Codosaur.us)
What can we evolve for you today?Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
--
*Senior Information Security Engineer OSCE, GWAPT, **CEH, **OSCP, **RHCE, *
*CCNA, **MCITP-EA*
Hi,
please do not post HTML emails to the mailing list. Thank you!
I must do a lot of verifications inside a def so i would like to know to
compact return and error notification
thanks
def self.doSomething(*args)
# Normal approach
if (0..4).include?index
puts "you has problem A"
return false
end
# Desired approach
return_with_info(false,"you has problem A") unless (0..4).include?index
end
This is a use case for exceptions, hence
raise "You have problem A" if (0..4).include? index
You would probably also want to define a specific exception class for this.
I hope that #index is a defined method, otherwise it would be an
undefined local variable and the code would not make sense.
On an even larger scope it would probably make sense to factor the
error checking into another method if this condition needs to be met
in many places, so rather
def self.ensure_index_valid
raise "You have problem A" if (0..4).include? index
end
and
def self.doSomething(*args)
ensure_index_valid
# more work
end
Cheers
robert
···
On Tue, Jan 2, 2018 at 2:42 AM, ng khanh <cnkhanh1986@gmail.com> wrote:
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/
I am very sorry - get used with forum and email before,
Do you have any recommendation to format code syntax for email list ?
my post is just brief description of def, in fact i have a lot different
(uncommon) exceptions inside each definitions
so specific expectation class would need input both message and conditions
Thanks
Khanh
···
On Tue, Jan 2, 2018 at 10:29 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
Hi,
please do not post HTML emails to the mailing list. Thank you!
On Tue, Jan 2, 2018 at 2:42 AM, ng khanh <cnkhanh1986@gmail.com> wrote:
> I must do a lot of verifications inside a def so i would like to know to
> compact return and error notification
> thanks
> def self.doSomething(*args)
> # Normal approach
> if (0..4).include?index
> puts "you has problem A"
> return false
> end
> # Desired approach
> return_with_info(false,"you has problem A") unless (0..4).include?index
> endThis is a use case for exceptions, hence
raise "You have problem A" if (0..4).include? index
You would probably also want to define a specific exception class for this.
I hope that #index is a defined method, otherwise it would be an
undefined local variable and the code would not make sense.On an even larger scope it would probably make sense to factor the
error checking into another method if this condition needs to be met
in many places, so ratherdef self.ensure_index_valid
raise "You have problem A" if (0..4).include? index
endand
def self.doSomething(*args)
ensure_index_valid# more work
endCheers
robert
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
I am very sorry - get used with forum and email before,
Do you have any recommendation to format code syntax for email list ?
Just send email in plain ASCII and insert the code as is.
my post is just brief description of def, in fact i have a lot different
(uncommon) exceptions inside each definitions
so specific expectation class would need input both message and conditions
I am not sure I understand this. Maybe you need to provide a
Kind regards
robert
···
On Tue, Jan 2, 2018 at 7:25 PM, ng khanh <cnkhanh1986@gmail.com> wrote:
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/
@Robert Klemme
Sorry for late reply, my first week after vacation was pretty busy.
Below is my pattern for custom definition usage. In general, i am still not
clear about which method (return false or raise) is best for my case.
In general, i don't want to stop running but need to get notification about
viable errors
Class Object
Def WorkOnSomething
#check condition 1:
check1 = some logical test
# first method
unless check1 # return false value
print "Error Description"
return false
end
# second method
raise "Error Description" unless check1 # return error and break progran
immediately
# check condition 2:
check2 = some other logical tests
# remain similar to first
.......
# Process something
.......
# check condition n:
checkn = some other logical tests
# remain similar to first
.........
return result
end #def
end #class
InputArray = ArrayofObject
puts InputArray.map {|x| x.WorkOnSomething}
···
On Wed, Jan 3, 2018 at 10:18 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Tue, Jan 2, 2018 at 7:25 PM, ng khanh <cnkhanh1986@gmail.com> wrote:
> I am very sorry - get used with forum and email before,
> Do you have any recommendation to format code syntax for email list ?Just send email in plain ASCII and insert the code as is.
> my post is just brief description of def, in fact i have a lot different
> (uncommon) exceptions inside each definitions
> so specific expectation class would need input both message and
conditionsI am not sure I understand this. Maybe you need to provide a
http://www.sscce.org/Kind regards
robert
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
This is not valid Ruby code and the lacking indentation makes it
unnecessary hard to digest. Apparently you put two approaches in one
method which does not make it more readable either. With those generic
names it is also difficult for me to get a grasp of what you are
trying to do: you want to do something, there can be errors and you
want to report these errors. That does not help decide whether you
should be using exceptions or something else.
Cheers
robert
···
On Sun, Jan 14, 2018 at 6:22 AM, ng khanh <cnkhanh1986@gmail.com> wrote:
@Robert Klemme
Sorry for late reply, my first week after vacation was pretty busy.
Below is my pattern for custom definition usage. In general, i am still not
clear about which method (return false or raise) is best for my case.
In general, i don't want to stop running but need to get notification about
viable errors
Class Object
Def WorkOnSomething
#check condition 1:
check1 = some logical test
# first method
unless check1 # return false value
print "Error Description"
return false
end
# second method
raise "Error Description" unless check1 # return error and break progran
immediately
# check condition 2:
check2 = some other logical tests
# remain similar to first
.......
# Process something
.......
# check condition n:
checkn = some other logical tests
# remain similar to first
.........
return result
end #def
end #class
InputArray = ArrayofObject
puts InputArray.map {|x| x.WorkOnSomething}
--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/