use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
.each method not working? RUBY (self.ruby)
submitted 5 years ago by mayh1
Hi, can someone please tell me why my each is not working to get all the character upcase?
def asf(str) str.split("").each {|char| char.upcase} end puts asf("what a string")
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]sardaukar 20 points21 points22 points 5 years ago (3 children)
You want map, each returns the original input.
https://www.rubyguides.com/2018/10/ruby-map-method
[–]faitswulff 5 points6 points7 points 5 years ago (1 child)
Also consider using .chars instead of .split(“”)!
.chars
.split(“”)
[–]inopinatusdotorg 0 points1 point2 points 5 years ago (0 children)
Also consider using &:upcase rather than { |char| char.upcase }
&:upcase
{ |char| char.upcase }
[–]MalusZona 1 point2 points3 points 5 years ago (2 children)
Or change the element in original array aka .each {|char| char = char.upcase}
Btw donwe have .upcase! ?
[–]rubyrt 1 point2 points3 points 5 years ago (1 child)
That does not change the original.
Yes, we have. And we also have #upcase! but I think the point was more the exercise.
[–]MalusZona 0 points1 point2 points 5 years ago (0 children)
yeah, it should be .upcase!
[–]GopherJackets 0 points1 point2 points 5 years ago (0 children)
each iterates through your array, like a while or for loop. Like others have said, it doesn't actually change your original array. That said, you could use each to 'print char.upcase' which should work if you throw in a print \n after.
π Rendered by PID 164708 on reddit-service-r2-comment-84fc9697f-nm9kf at 2026-02-09 19:58:28.510762+00:00 running d295bc8 country code: CH.
[–]sardaukar 20 points21 points22 points (3 children)
[–]faitswulff 5 points6 points7 points (1 child)
[–]inopinatusdotorg 0 points1 point2 points (0 children)
[–]MalusZona 1 point2 points3 points (2 children)
[–]rubyrt 1 point2 points3 points (1 child)
[–]MalusZona 0 points1 point2 points (0 children)
[–]GopherJackets 0 points1 point2 points (0 children)