Determine which value was selected
I have three numerical variables: on_the_ground
, double_round_trip
and point_to_point
. The price
function returns one of these based on some simple logic.
Below is how it currently works.
def price
return on_the_ground if date_range == 1
values = [
on_the_ground,
double_round_trip
]
if !turbo? && !vlj?
values.push(point_to_point)
end
values.compact.min
end
I'd like to have a function that can return a symbol based on which value should be returned. For example:
def price_name
return :on_the_ground if date_range == 1
... etc...
end
ruby algorithm logic
add a comment |
I have three numerical variables: on_the_ground
, double_round_trip
and point_to_point
. The price
function returns one of these based on some simple logic.
Below is how it currently works.
def price
return on_the_ground if date_range == 1
values = [
on_the_ground,
double_round_trip
]
if !turbo? && !vlj?
values.push(point_to_point)
end
values.compact.min
end
I'd like to have a function that can return a symbol based on which value should be returned. For example:
def price_name
return :on_the_ground if date_range == 1
... etc...
end
ruby algorithm logic
A method can return aSymbol
, just like theprice_name
method above does. What exactly is the issue?
– Rohit Namjoshi
Nov 20 '18 at 4:12
I need to get the name of the variable thatvalues.compact.min
chose.
– zcreative
Nov 20 '18 at 4:21
To do that you need a mapping between values and names. UseHash
to do that.
– Rohit Namjoshi
Nov 20 '18 at 5:01
add a comment |
I have three numerical variables: on_the_ground
, double_round_trip
and point_to_point
. The price
function returns one of these based on some simple logic.
Below is how it currently works.
def price
return on_the_ground if date_range == 1
values = [
on_the_ground,
double_round_trip
]
if !turbo? && !vlj?
values.push(point_to_point)
end
values.compact.min
end
I'd like to have a function that can return a symbol based on which value should be returned. For example:
def price_name
return :on_the_ground if date_range == 1
... etc...
end
ruby algorithm logic
I have three numerical variables: on_the_ground
, double_round_trip
and point_to_point
. The price
function returns one of these based on some simple logic.
Below is how it currently works.
def price
return on_the_ground if date_range == 1
values = [
on_the_ground,
double_round_trip
]
if !turbo? && !vlj?
values.push(point_to_point)
end
values.compact.min
end
I'd like to have a function that can return a symbol based on which value should be returned. For example:
def price_name
return :on_the_ground if date_range == 1
... etc...
end
ruby algorithm logic
ruby algorithm logic
asked Nov 20 '18 at 3:31
zcreativezcreative
17
17
A method can return aSymbol
, just like theprice_name
method above does. What exactly is the issue?
– Rohit Namjoshi
Nov 20 '18 at 4:12
I need to get the name of the variable thatvalues.compact.min
chose.
– zcreative
Nov 20 '18 at 4:21
To do that you need a mapping between values and names. UseHash
to do that.
– Rohit Namjoshi
Nov 20 '18 at 5:01
add a comment |
A method can return aSymbol
, just like theprice_name
method above does. What exactly is the issue?
– Rohit Namjoshi
Nov 20 '18 at 4:12
I need to get the name of the variable thatvalues.compact.min
chose.
– zcreative
Nov 20 '18 at 4:21
To do that you need a mapping between values and names. UseHash
to do that.
– Rohit Namjoshi
Nov 20 '18 at 5:01
A method can return a
Symbol
, just like the price_name
method above does. What exactly is the issue?– Rohit Namjoshi
Nov 20 '18 at 4:12
A method can return a
Symbol
, just like the price_name
method above does. What exactly is the issue?– Rohit Namjoshi
Nov 20 '18 at 4:12
I need to get the name of the variable that
values.compact.min
chose.– zcreative
Nov 20 '18 at 4:21
I need to get the name of the variable that
values.compact.min
chose.– zcreative
Nov 20 '18 at 4:21
To do that you need a mapping between values and names. Use
Hash
to do that.– Rohit Namjoshi
Nov 20 '18 at 5:01
To do that you need a mapping between values and names. Use
Hash
to do that.– Rohit Namjoshi
Nov 20 '18 at 5:01
add a comment |
3 Answers
3
active
oldest
votes
If I get your point, why not use an array?
def price_name_for date_range
[:on_the_ground, :double_round_trip, :point_to_point][date_range-1]
end
price_name_for 1 #=> :on_the_ground
price_name_for 2 #=> :double_round_trip
price_name_for 3 #=> :point_to_point
It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.
– zcreative
Nov 20 '18 at 7:14
add a comment |
you could use case
case values
when double_round_trip
return :double_round_trip
when on_the_ground
...
https://www.rubyguides.com/2015/10/ruby-case/
add a comment |
I ended up using:
price_options.key(price) # double_round_trip
Using aHash
is what I suggested in my comment.
– Rohit Namjoshi
Nov 20 '18 at 15:42
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385814%2fdetermine-which-value-was-selected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I get your point, why not use an array?
def price_name_for date_range
[:on_the_ground, :double_round_trip, :point_to_point][date_range-1]
end
price_name_for 1 #=> :on_the_ground
price_name_for 2 #=> :double_round_trip
price_name_for 3 #=> :point_to_point
It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.
– zcreative
Nov 20 '18 at 7:14
add a comment |
If I get your point, why not use an array?
def price_name_for date_range
[:on_the_ground, :double_round_trip, :point_to_point][date_range-1]
end
price_name_for 1 #=> :on_the_ground
price_name_for 2 #=> :double_round_trip
price_name_for 3 #=> :point_to_point
It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.
– zcreative
Nov 20 '18 at 7:14
add a comment |
If I get your point, why not use an array?
def price_name_for date_range
[:on_the_ground, :double_round_trip, :point_to_point][date_range-1]
end
price_name_for 1 #=> :on_the_ground
price_name_for 2 #=> :double_round_trip
price_name_for 3 #=> :point_to_point
If I get your point, why not use an array?
def price_name_for date_range
[:on_the_ground, :double_round_trip, :point_to_point][date_range-1]
end
price_name_for 1 #=> :on_the_ground
price_name_for 2 #=> :double_round_trip
price_name_for 3 #=> :point_to_point
answered Nov 20 '18 at 6:12
iGianiGian
4,2692623
4,2692623
It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.
– zcreative
Nov 20 '18 at 7:14
add a comment |
It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.
– zcreative
Nov 20 '18 at 7:14
It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.
– zcreative
Nov 20 '18 at 7:14
It's a little more complicated than that. There are some conditions for the point to point price to be in the array. I've provided an answer below showing how I solved it.
– zcreative
Nov 20 '18 at 7:14
add a comment |
you could use case
case values
when double_round_trip
return :double_round_trip
when on_the_ground
...
https://www.rubyguides.com/2015/10/ruby-case/
add a comment |
you could use case
case values
when double_round_trip
return :double_round_trip
when on_the_ground
...
https://www.rubyguides.com/2015/10/ruby-case/
add a comment |
you could use case
case values
when double_round_trip
return :double_round_trip
when on_the_ground
...
https://www.rubyguides.com/2015/10/ruby-case/
you could use case
case values
when double_round_trip
return :double_round_trip
when on_the_ground
...
https://www.rubyguides.com/2015/10/ruby-case/
answered Nov 20 '18 at 4:19
Hector Moreno-BravoHector Moreno-Bravo
12
12
add a comment |
add a comment |
I ended up using:
price_options.key(price) # double_round_trip
Using aHash
is what I suggested in my comment.
– Rohit Namjoshi
Nov 20 '18 at 15:42
add a comment |
I ended up using:
price_options.key(price) # double_round_trip
Using aHash
is what I suggested in my comment.
– Rohit Namjoshi
Nov 20 '18 at 15:42
add a comment |
I ended up using:
price_options.key(price) # double_round_trip
I ended up using:
price_options.key(price) # double_round_trip
answered Nov 20 '18 at 5:09
zcreativezcreative
17
17
Using aHash
is what I suggested in my comment.
– Rohit Namjoshi
Nov 20 '18 at 15:42
add a comment |
Using aHash
is what I suggested in my comment.
– Rohit Namjoshi
Nov 20 '18 at 15:42
Using a
Hash
is what I suggested in my comment.– Rohit Namjoshi
Nov 20 '18 at 15:42
Using a
Hash
is what I suggested in my comment.– Rohit Namjoshi
Nov 20 '18 at 15:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385814%2fdetermine-which-value-was-selected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
A method can return a
Symbol
, just like theprice_name
method above does. What exactly is the issue?– Rohit Namjoshi
Nov 20 '18 at 4:12
I need to get the name of the variable that
values.compact.min
chose.– zcreative
Nov 20 '18 at 4:21
To do that you need a mapping between values and names. Use
Hash
to do that.– Rohit Namjoshi
Nov 20 '18 at 5:01