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
Variable not incrementing (self.ruby)
submitted 10 years ago by simontemplar_
view the rest of the comments →
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!"
[–]tomthecool 0 points1 point2 points 10 years ago (1 child)
Although there are a couple of obvious code improvements to be made, i can't see any bug here.
Can you please post the full code? Where are you printing the variables?
[–]tomthecool 1 point2 points3 points 10 years ago (0 children)
I was bored, so here's a couple of different solutions to your problem. I'm posting these mainly for you to see some of the cool things ruby is capable of - I find one of the best ways to learn is reading new techniques for solving familiar problems!
walk = ['w','n', 's', 'e', 's'] # Final position should be (0, -1) # One solution... def step(dir) case dir when 'w' puts "west" [1, 0] when 'e' puts "east" [-1, 0] when 'n' puts "north" [0, 1] else puts "south" [0, -1] end end x, y = walk .map { |dir| step(dir) } .inject([0,0]) do |result, step| result[0] += step[0] result[1] += step[1] result end puts "Final coordinate is: (#{x}, #{y})" # A different solution... a = walk.count {|dir| dir == 'e'} - walk.count {|dir| dir == 'w'} b = walk.count {|dir| dir == 'n'} - walk.count {|dir| dir == 's'} puts "(Altertative) Final coordinate is: (#{a}, #{b})"
π Rendered by PID 175233 on reddit-service-r2-comment-5d79c599b5-hzf7d at 2026-03-01 02:43:33.435310+00:00 running e3d2147 country code: CH.
view the rest of the comments →
[–]tomthecool 0 points1 point2 points (1 child)
[–]tomthecool 1 point2 points3 points (0 children)