Prev: Thread error "undefined method `keys' for nil:NilClass"
Next: Getting version from PE executables
From: Marnen Laibow-Koser on 4 Nov 2009 18:18 Michael W. Ryder wrote: > Walton Hoops wrote: > <snip> > >> Now consider the ruby way: >> >> 10.times do |i| >> print "#{i}," >> end >> >> Some length as the C code, but much more readable. Heck, it's >> almost English! > > Not for me it wasn't. I had to try it to see that it actually works. > My initial impression was that it would print 10 copies of i. I still > don't see where 'i' is incremented Well, you do have to know what Numeric#times yields to its block. But that's easy to look up. (However, it starts from 0, so it's not quite equivalent to the C.) > so this is one of those "magical" > constructs much like your impression of ++ in C. No. The "magic" is different. In the Ruby version, a quick check in the API tells you that the counter is incremented each time through the loop. In the C version, OTOH, you have to think about exactly where you've put the ++, and whether you really wanted the value before or after the increment. > I would find this much > harder to maintain than the C version. Only because you apparently are not familiar with common Ruby idioms. The Ruby version has a lot less to go wrong in it, because the language transparently handles incrementing the "loop index" at the right time. > > Which is part of the beauty of Ruby: It's simple, Yes. Just don't expect it to be much like C. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen(a)marnen.org -- Posted via http://www.ruby-forum.com/.
From: Walton Hoops on 4 Nov 2009 18:48 > -----Original Message----- > From: marnen(a)marnen.org [mailto:marnen(a)marnen.org] > >> Now consider the ruby way: > >> > >> 10.times do |i| > >> print "#{i}," > >> end > >> > >> Some length as the C code, but much more readable. Heck, it's > >> almost English! > > > > Well, you do have to know what Numeric#times yields to its block. But > that's easy to look up. (However, it starts from 0, so it's not quite > equivalent to the C.) Whoop! Good point, that's what I get for not actually testing my code. Corrected (and even closer to English). (1..9).each do |i| print "#{i}," end Michael Wrote: > > Not for me it wasn't. I had to try it to see that it actually works. > > My initial impression was that it would print 10 copies of i. > I still don't see where 'i' is incremented It isn't incremented, at least not in MY code (if you must think in terms of incrementing variables, then Ruby is incrementing it for me) See the times method at: http://ruby-doc.org/core/classes/Integer.html That kind of looping is a pretty core Ruby concept. Also the corrected version I wrote above should be a bit clearer. Also I think your previous programming experience is hurting you here. To you as (I'm guessing) a C programmer, to progress in a loop, you must increment modify variable. The average English speaker doesn't think in those terms. Looking at my most recent example, the English equivalent would be for each 'i' from 1 to 9 print 'i' followed by a comma. Sure, the words may not be in the precise order, but it comes a darn site closer to natural language than: int i=1; while (i<10) { printf("%d,",i++); }
From: Michael W. Ryder on 4 Nov 2009 19:08 Walton Hoops wrote: >> -----Original Message----- >> From: marnen(a)marnen.org [mailto:marnen(a)marnen.org] >>>> Now consider the ruby way: >>>> >>>> 10.times do |i| >>>> print "#{i}," >>>> end >>>> >>>> Some length as the C code, but much more readable. Heck, it's >>>> almost English! >> Well, you do have to know what Numeric#times yields to its block. But >> that's easy to look up. (However, it starts from 0, so it's not quite >> equivalent to the C.) > > Whoop! Good point, that's what I get for not actually testing my code. > > Corrected (and even closer to English). > > (1..9).each do |i| > print "#{i}," > end > That version I understand just looking at it as it is equivalent to a for loop. Your first version seemed more "magical" since I don't know where 'i' is getting incremented. At least with C I know where the incrementing is occurring. > Michael Wrote: >>> Not for me it wasn't. I had to try it to see that it actually works. >>> My initial impression was that it would print 10 copies of i. >> I still don't see where 'i' is incremented > > It isn't incremented, at least not in MY code (if you must think in > terms of incrementing variables, then Ruby is incrementing it for me) > See the times method at: > http://ruby-doc.org/core/classes/Integer.html > That kind of looping is a pretty core Ruby concept. > > Also the corrected version I wrote above should be a bit clearer. Also > I think your previous programming experience is hurting you here. To you > as (I'm guessing) a C programmer, to progress in a loop, you must increment > modify variable. The average English speaker doesn't think in those terms. > I started with Fortran in the early 1980's, followed by Basic, Pascal, Modula 2, and C. For over 25 years I have been mostly programming in Business Basic. While Ruby has a lot of things going for it I miss some of the features available in the other languages, especially the built in curses and file handling in Business Basic. > Looking at my most recent example, the English equivalent would be for each > 'i' from 1 to 9 print 'i' followed by a comma. Sure, the words may not be > in the precise order, but it comes a darn site closer to natural language than: > > int i=1; > while (i<10) > { > printf("%d,",i++); > } > > >
From: Gavin Sinclair on 5 Nov 2009 05:38 On Nov 5, 4:11 am, Tony Arcieri <t...(a)medioh.com> wrote: > > I think you're missing why ++ could be useful, and it's precisely because > Ruby is a "21st century language" > > The ++ operator, far more than just being syntactic sugar for +=1, would > allow you to send an "increment" message to any object, which would change > its value in place, i.e. > > def ++ > incrementing_logic_goes_here > end > > I could see this as being handy What's wrong with def inc incrementing_logic_goes_here end How is that any different?
From: Gavin Sinclair on 5 Nov 2009 05:42
On Nov 5, 7:22 am, "Michael W. Ryder" <_mwryde...(a)gmail.com> wrote: > > But i.succ does Not work in the following: > > i = 1 > while (i < 10) > puts i.succ > end > > the only way to get this to work is to use: > puts i; i = i.succ > > which is not as clean as using puts i++. Maybe you'll just have to find another way to print the numbers 1 to 10 ;) |