How can I implement a chain method for my class?
I'm working with a string that has to be converted to a 2 dimensional array:
rows = [['1', '2'], ['10', '20']]
I need those values as integers instead of strings. I can iterate through them and then do map
, like:
rows.each {|row| row.map!(&:to_i)}
I was trying to create a to_i
method that could be chainable, so I can run rows.to_i
.
def to_i
each do |nested|
nested.map!(&:to_i)
end
end
This unsurprisingly fails:
NoMethodError: undefined method `to_i' for [["1", "2"], ["10", "20"]]:Array
since Array
doesn't implement the method. Besides monkey patching the Array
class, is there a proper or more rubier way to do it?
The other alternative is doing:
def ary_to_i array
array.each do |nested|
nested.map!(&:to_i)
end
end
but I found the method call ary_to_i(rows)
confusing.
arrays ruby type-conversion
add a comment |
I'm working with a string that has to be converted to a 2 dimensional array:
rows = [['1', '2'], ['10', '20']]
I need those values as integers instead of strings. I can iterate through them and then do map
, like:
rows.each {|row| row.map!(&:to_i)}
I was trying to create a to_i
method that could be chainable, so I can run rows.to_i
.
def to_i
each do |nested|
nested.map!(&:to_i)
end
end
This unsurprisingly fails:
NoMethodError: undefined method `to_i' for [["1", "2"], ["10", "20"]]:Array
since Array
doesn't implement the method. Besides monkey patching the Array
class, is there a proper or more rubier way to do it?
The other alternative is doing:
def ary_to_i array
array.each do |nested|
nested.map!(&:to_i)
end
end
but I found the method call ary_to_i(rows)
confusing.
arrays ruby type-conversion
1
Wouldn't it be possible to do the string->int casting somewhere before, where it's easier? We don't see the whole method, so it's just a guess, but it's something I'd look for.
– Marcin Kołodziej
Nov 25 '18 at 16:35
1
I would say the Ruby way is to abandon your quest. Even if you were agreeable to adding a methodto_i
toArray
it would only apply to elements of arrays that are strings, which would be ugly beyond words.
– Cary Swoveland
Nov 25 '18 at 18:27
add a comment |
I'm working with a string that has to be converted to a 2 dimensional array:
rows = [['1', '2'], ['10', '20']]
I need those values as integers instead of strings. I can iterate through them and then do map
, like:
rows.each {|row| row.map!(&:to_i)}
I was trying to create a to_i
method that could be chainable, so I can run rows.to_i
.
def to_i
each do |nested|
nested.map!(&:to_i)
end
end
This unsurprisingly fails:
NoMethodError: undefined method `to_i' for [["1", "2"], ["10", "20"]]:Array
since Array
doesn't implement the method. Besides monkey patching the Array
class, is there a proper or more rubier way to do it?
The other alternative is doing:
def ary_to_i array
array.each do |nested|
nested.map!(&:to_i)
end
end
but I found the method call ary_to_i(rows)
confusing.
arrays ruby type-conversion
I'm working with a string that has to be converted to a 2 dimensional array:
rows = [['1', '2'], ['10', '20']]
I need those values as integers instead of strings. I can iterate through them and then do map
, like:
rows.each {|row| row.map!(&:to_i)}
I was trying to create a to_i
method that could be chainable, so I can run rows.to_i
.
def to_i
each do |nested|
nested.map!(&:to_i)
end
end
This unsurprisingly fails:
NoMethodError: undefined method `to_i' for [["1", "2"], ["10", "20"]]:Array
since Array
doesn't implement the method. Besides monkey patching the Array
class, is there a proper or more rubier way to do it?
The other alternative is doing:
def ary_to_i array
array.each do |nested|
nested.map!(&:to_i)
end
end
but I found the method call ary_to_i(rows)
confusing.
arrays ruby type-conversion
arrays ruby type-conversion
edited Nov 26 '18 at 4:41
sawa
132k29205305
132k29205305
asked Nov 25 '18 at 16:22
Pablo Olmos de Aguilera C.Pablo Olmos de Aguilera C.
2,71212733
2,71212733
1
Wouldn't it be possible to do the string->int casting somewhere before, where it's easier? We don't see the whole method, so it's just a guess, but it's something I'd look for.
– Marcin Kołodziej
Nov 25 '18 at 16:35
1
I would say the Ruby way is to abandon your quest. Even if you were agreeable to adding a methodto_i
toArray
it would only apply to elements of arrays that are strings, which would be ugly beyond words.
– Cary Swoveland
Nov 25 '18 at 18:27
add a comment |
1
Wouldn't it be possible to do the string->int casting somewhere before, where it's easier? We don't see the whole method, so it's just a guess, but it's something I'd look for.
– Marcin Kołodziej
Nov 25 '18 at 16:35
1
I would say the Ruby way is to abandon your quest. Even if you were agreeable to adding a methodto_i
toArray
it would only apply to elements of arrays that are strings, which would be ugly beyond words.
– Cary Swoveland
Nov 25 '18 at 18:27
1
1
Wouldn't it be possible to do the string->int casting somewhere before, where it's easier? We don't see the whole method, so it's just a guess, but it's something I'd look for.
– Marcin Kołodziej
Nov 25 '18 at 16:35
Wouldn't it be possible to do the string->int casting somewhere before, where it's easier? We don't see the whole method, so it's just a guess, but it's something I'd look for.
– Marcin Kołodziej
Nov 25 '18 at 16:35
1
1
I would say the Ruby way is to abandon your quest. Even if you were agreeable to adding a method
to_i
to Array
it would only apply to elements of arrays that are strings, which would be ugly beyond words.– Cary Swoveland
Nov 25 '18 at 18:27
I would say the Ruby way is to abandon your quest. Even if you were agreeable to adding a method
to_i
to Array
it would only apply to elements of arrays that are strings, which would be ugly beyond words.– Cary Swoveland
Nov 25 '18 at 18:27
add a comment |
2 Answers
2
active
oldest
votes
You could define a to_i
method just for rows:
rows = [['1','2'], ['10','20']]
def rows.to_i
each do |nested|
nested.map!(&:to_i)
end
end
p rows.to_i # => [[1, 2], [10, 20]]
add a comment |
Although it’s an anti-pattern, you might monkey-patch the Array
class:
class Array
def to_i
map do |nested|
nested.map(&:to_i)
end
end
def to_i!
each do |nested|
nested.map!(&:to_i)
end
end
end
But I’d better go with the latter alternative in OP.
Sidenote: mutating arrays is not a best idea ever, but if you do, choose the name ending with a bang as in my example.
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%2f53469465%2fhow-can-i-implement-a-chain-method-for-my-class%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 could define a to_i
method just for rows:
rows = [['1','2'], ['10','20']]
def rows.to_i
each do |nested|
nested.map!(&:to_i)
end
end
p rows.to_i # => [[1, 2], [10, 20]]
add a comment |
You could define a to_i
method just for rows:
rows = [['1','2'], ['10','20']]
def rows.to_i
each do |nested|
nested.map!(&:to_i)
end
end
p rows.to_i # => [[1, 2], [10, 20]]
add a comment |
You could define a to_i
method just for rows:
rows = [['1','2'], ['10','20']]
def rows.to_i
each do |nested|
nested.map!(&:to_i)
end
end
p rows.to_i # => [[1, 2], [10, 20]]
You could define a to_i
method just for rows:
rows = [['1','2'], ['10','20']]
def rows.to_i
each do |nested|
nested.map!(&:to_i)
end
end
p rows.to_i # => [[1, 2], [10, 20]]
answered Nov 25 '18 at 16:50
steenslagsteenslag
63k11102142
63k11102142
add a comment |
add a comment |
Although it’s an anti-pattern, you might monkey-patch the Array
class:
class Array
def to_i
map do |nested|
nested.map(&:to_i)
end
end
def to_i!
each do |nested|
nested.map!(&:to_i)
end
end
end
But I’d better go with the latter alternative in OP.
Sidenote: mutating arrays is not a best idea ever, but if you do, choose the name ending with a bang as in my example.
add a comment |
Although it’s an anti-pattern, you might monkey-patch the Array
class:
class Array
def to_i
map do |nested|
nested.map(&:to_i)
end
end
def to_i!
each do |nested|
nested.map!(&:to_i)
end
end
end
But I’d better go with the latter alternative in OP.
Sidenote: mutating arrays is not a best idea ever, but if you do, choose the name ending with a bang as in my example.
add a comment |
Although it’s an anti-pattern, you might monkey-patch the Array
class:
class Array
def to_i
map do |nested|
nested.map(&:to_i)
end
end
def to_i!
each do |nested|
nested.map!(&:to_i)
end
end
end
But I’d better go with the latter alternative in OP.
Sidenote: mutating arrays is not a best idea ever, but if you do, choose the name ending with a bang as in my example.
Although it’s an anti-pattern, you might monkey-patch the Array
class:
class Array
def to_i
map do |nested|
nested.map(&:to_i)
end
end
def to_i!
each do |nested|
nested.map!(&:to_i)
end
end
end
But I’d better go with the latter alternative in OP.
Sidenote: mutating arrays is not a best idea ever, but if you do, choose the name ending with a bang as in my example.
answered Nov 25 '18 at 16:27
Aleksei MatiushkinAleksei Matiushkin
83.6k95694
83.6k95694
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%2f53469465%2fhow-can-i-implement-a-chain-method-for-my-class%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
1
Wouldn't it be possible to do the string->int casting somewhere before, where it's easier? We don't see the whole method, so it's just a guess, but it's something I'd look for.
– Marcin Kołodziej
Nov 25 '18 at 16:35
1
I would say the Ruby way is to abandon your quest. Even if you were agreeable to adding a method
to_i
toArray
it would only apply to elements of arrays that are strings, which would be ugly beyond words.– Cary Swoveland
Nov 25 '18 at 18:27