Ruby - Option within parameter
I am having trouble understand options with ruby.
def most_frequent_kmers(opt={})
str = opt[:str]
min_chunk_size = opt[:min_chunk_size] || 1
max_chunk_size = opt[:max_chunk_size] || str.length - 1
min_occurences = opt[:min_occurences] || 1
results = {}
top_scoring = {}
end
most_frequent_kmers(1)
which gives me an error of
`': no implicit conversion of Symbol into Integer (TypeError)
I am not sure what to do to solve this.
ruby
add a comment |
I am having trouble understand options with ruby.
def most_frequent_kmers(opt={})
str = opt[:str]
min_chunk_size = opt[:min_chunk_size] || 1
max_chunk_size = opt[:max_chunk_size] || str.length - 1
min_occurences = opt[:min_occurences] || 1
results = {}
top_scoring = {}
end
most_frequent_kmers(1)
which gives me an error of
`': no implicit conversion of Symbol into Integer (TypeError)
I am not sure what to do to solve this.
ruby
It's true that3 || 2 - 1 #=> 3
, but readers of your code you appreciate parentheses:3 || (2 - 1)
. When you report an error in a SO question please indicate the line of code where it occurred as well as the message. Note that your method would never be written that way asresults
andtop_scoring
are both local variables that have no purpose and whose values are lost when the method is exited.
– Cary Swoveland
Nov 15 '18 at 4:58
add a comment |
I am having trouble understand options with ruby.
def most_frequent_kmers(opt={})
str = opt[:str]
min_chunk_size = opt[:min_chunk_size] || 1
max_chunk_size = opt[:max_chunk_size] || str.length - 1
min_occurences = opt[:min_occurences] || 1
results = {}
top_scoring = {}
end
most_frequent_kmers(1)
which gives me an error of
`': no implicit conversion of Symbol into Integer (TypeError)
I am not sure what to do to solve this.
ruby
I am having trouble understand options with ruby.
def most_frequent_kmers(opt={})
str = opt[:str]
min_chunk_size = opt[:min_chunk_size] || 1
max_chunk_size = opt[:max_chunk_size] || str.length - 1
min_occurences = opt[:min_occurences] || 1
results = {}
top_scoring = {}
end
most_frequent_kmers(1)
which gives me an error of
`': no implicit conversion of Symbol into Integer (TypeError)
I am not sure what to do to solve this.
ruby
ruby
asked Nov 14 '18 at 23:21
dpopodpopo
245
245
It's true that3 || 2 - 1 #=> 3
, but readers of your code you appreciate parentheses:3 || (2 - 1)
. When you report an error in a SO question please indicate the line of code where it occurred as well as the message. Note that your method would never be written that way asresults
andtop_scoring
are both local variables that have no purpose and whose values are lost when the method is exited.
– Cary Swoveland
Nov 15 '18 at 4:58
add a comment |
It's true that3 || 2 - 1 #=> 3
, but readers of your code you appreciate parentheses:3 || (2 - 1)
. When you report an error in a SO question please indicate the line of code where it occurred as well as the message. Note that your method would never be written that way asresults
andtop_scoring
are both local variables that have no purpose and whose values are lost when the method is exited.
– Cary Swoveland
Nov 15 '18 at 4:58
It's true that
3 || 2 - 1 #=> 3
, but readers of your code you appreciate parentheses: 3 || (2 - 1)
. When you report an error in a SO question please indicate the line of code where it occurred as well as the message. Note that your method would never be written that way as results
and top_scoring
are both local variables that have no purpose and whose values are lost when the method is exited.– Cary Swoveland
Nov 15 '18 at 4:58
It's true that
3 || 2 - 1 #=> 3
, but readers of your code you appreciate parentheses: 3 || (2 - 1)
. When you report an error in a SO question please indicate the line of code where it occurred as well as the message. Note that your method would never be written that way as results
and top_scoring
are both local variables that have no purpose and whose values are lost when the method is exited.– Cary Swoveland
Nov 15 '18 at 4:58
add a comment |
2 Answers
2
active
oldest
votes
you should pass instead to most_frequent_kmers
a hash like that:
# depends on the ruby version you are using
# {str: "hey"} and {:str => "hey"} work also
most_frequent_kmers(str: "hey")
add a comment |
opts
means you can pass an "unlimited" number of arguments when calling the function, but all of them should be named, as you can see in the method's body:
str = opt[:str]
min_chunk_size = opt[:min_chunk_size] || 1
max_chunk_size = opt[:max_chunk_size] || str.length - 1
min_occurences = opt[:min_occurences] || 1
...
It's assigning the value of the parameter str within the opt one, min_chunk_size, and so on. But in the case of str, this is the only one that doesn't have a "default" value, but even so, this way max_chunk_size
depends on this, when that value as argument isn't provided (since the str.length - 1 assignment).
For making use most_frequent_kmers
you need to provided a String object, as the str argument (really I think it should be a String, as per the name - str). So this way the logic inside is able to keep working, all other local variables in it have default values if they're not provided.
If you want to pass str
as argument, you can just do most_frequent_kmers(str: 'Some String')
, if you don't then it'll return a NoMethodError
, since opt[:str]
will be nil
, and the "fallback" value for when that happens will try to invoke the length
method on nil
.
And tl;dr;, since you're just passing an Integer as argument, Ruby tries to invoke on the opts argument, raising the TypeError for trying an implicit conversion since
Integer#
expects to receive an Integer as argument, and you're passing a symbol.
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%2f53310249%2fruby-option-within-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
you should pass instead to most_frequent_kmers
a hash like that:
# depends on the ruby version you are using
# {str: "hey"} and {:str => "hey"} work also
most_frequent_kmers(str: "hey")
add a comment |
you should pass instead to most_frequent_kmers
a hash like that:
# depends on the ruby version you are using
# {str: "hey"} and {:str => "hey"} work also
most_frequent_kmers(str: "hey")
add a comment |
you should pass instead to most_frequent_kmers
a hash like that:
# depends on the ruby version you are using
# {str: "hey"} and {:str => "hey"} work also
most_frequent_kmers(str: "hey")
you should pass instead to most_frequent_kmers
a hash like that:
# depends on the ruby version you are using
# {str: "hey"} and {:str => "hey"} work also
most_frequent_kmers(str: "hey")
edited Nov 14 '18 at 23:33
answered Nov 14 '18 at 23:28
Constantin GuidonConstantin Guidon
1,112817
1,112817
add a comment |
add a comment |
opts
means you can pass an "unlimited" number of arguments when calling the function, but all of them should be named, as you can see in the method's body:
str = opt[:str]
min_chunk_size = opt[:min_chunk_size] || 1
max_chunk_size = opt[:max_chunk_size] || str.length - 1
min_occurences = opt[:min_occurences] || 1
...
It's assigning the value of the parameter str within the opt one, min_chunk_size, and so on. But in the case of str, this is the only one that doesn't have a "default" value, but even so, this way max_chunk_size
depends on this, when that value as argument isn't provided (since the str.length - 1 assignment).
For making use most_frequent_kmers
you need to provided a String object, as the str argument (really I think it should be a String, as per the name - str). So this way the logic inside is able to keep working, all other local variables in it have default values if they're not provided.
If you want to pass str
as argument, you can just do most_frequent_kmers(str: 'Some String')
, if you don't then it'll return a NoMethodError
, since opt[:str]
will be nil
, and the "fallback" value for when that happens will try to invoke the length
method on nil
.
And tl;dr;, since you're just passing an Integer as argument, Ruby tries to invoke on the opts argument, raising the TypeError for trying an implicit conversion since
Integer#
expects to receive an Integer as argument, and you're passing a symbol.
add a comment |
opts
means you can pass an "unlimited" number of arguments when calling the function, but all of them should be named, as you can see in the method's body:
str = opt[:str]
min_chunk_size = opt[:min_chunk_size] || 1
max_chunk_size = opt[:max_chunk_size] || str.length - 1
min_occurences = opt[:min_occurences] || 1
...
It's assigning the value of the parameter str within the opt one, min_chunk_size, and so on. But in the case of str, this is the only one that doesn't have a "default" value, but even so, this way max_chunk_size
depends on this, when that value as argument isn't provided (since the str.length - 1 assignment).
For making use most_frequent_kmers
you need to provided a String object, as the str argument (really I think it should be a String, as per the name - str). So this way the logic inside is able to keep working, all other local variables in it have default values if they're not provided.
If you want to pass str
as argument, you can just do most_frequent_kmers(str: 'Some String')
, if you don't then it'll return a NoMethodError
, since opt[:str]
will be nil
, and the "fallback" value for when that happens will try to invoke the length
method on nil
.
And tl;dr;, since you're just passing an Integer as argument, Ruby tries to invoke on the opts argument, raising the TypeError for trying an implicit conversion since
Integer#
expects to receive an Integer as argument, and you're passing a symbol.
add a comment |
opts
means you can pass an "unlimited" number of arguments when calling the function, but all of them should be named, as you can see in the method's body:
str = opt[:str]
min_chunk_size = opt[:min_chunk_size] || 1
max_chunk_size = opt[:max_chunk_size] || str.length - 1
min_occurences = opt[:min_occurences] || 1
...
It's assigning the value of the parameter str within the opt one, min_chunk_size, and so on. But in the case of str, this is the only one that doesn't have a "default" value, but even so, this way max_chunk_size
depends on this, when that value as argument isn't provided (since the str.length - 1 assignment).
For making use most_frequent_kmers
you need to provided a String object, as the str argument (really I think it should be a String, as per the name - str). So this way the logic inside is able to keep working, all other local variables in it have default values if they're not provided.
If you want to pass str
as argument, you can just do most_frequent_kmers(str: 'Some String')
, if you don't then it'll return a NoMethodError
, since opt[:str]
will be nil
, and the "fallback" value for when that happens will try to invoke the length
method on nil
.
And tl;dr;, since you're just passing an Integer as argument, Ruby tries to invoke on the opts argument, raising the TypeError for trying an implicit conversion since
Integer#
expects to receive an Integer as argument, and you're passing a symbol.
opts
means you can pass an "unlimited" number of arguments when calling the function, but all of them should be named, as you can see in the method's body:
str = opt[:str]
min_chunk_size = opt[:min_chunk_size] || 1
max_chunk_size = opt[:max_chunk_size] || str.length - 1
min_occurences = opt[:min_occurences] || 1
...
It's assigning the value of the parameter str within the opt one, min_chunk_size, and so on. But in the case of str, this is the only one that doesn't have a "default" value, but even so, this way max_chunk_size
depends on this, when that value as argument isn't provided (since the str.length - 1 assignment).
For making use most_frequent_kmers
you need to provided a String object, as the str argument (really I think it should be a String, as per the name - str). So this way the logic inside is able to keep working, all other local variables in it have default values if they're not provided.
If you want to pass str
as argument, you can just do most_frequent_kmers(str: 'Some String')
, if you don't then it'll return a NoMethodError
, since opt[:str]
will be nil
, and the "fallback" value for when that happens will try to invoke the length
method on nil
.
And tl;dr;, since you're just passing an Integer as argument, Ruby tries to invoke on the opts argument, raising the TypeError for trying an implicit conversion since
Integer#
expects to receive an Integer as argument, and you're passing a symbol.
answered Nov 14 '18 at 23:58
Sebastian PalmaSebastian Palma
15.7k41933
15.7k41933
add a comment |
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%2f53310249%2fruby-option-within-parameter%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
It's true that
3 || 2 - 1 #=> 3
, but readers of your code you appreciate parentheses:3 || (2 - 1)
. When you report an error in a SO question please indicate the line of code where it occurred as well as the message. Note that your method would never be written that way asresults
andtop_scoring
are both local variables that have no purpose and whose values are lost when the method is exited.– Cary Swoveland
Nov 15 '18 at 4:58