hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

The Principle of Hard Not to Understand #

by why in cult

Chang Sau Sheong: Ruby has an elegant form to it that is simply a pleasure to program in. A simple example of this is the way Ruby treats everything as objects, manifested by beautiful statements such as:

 5.times do |i|
   puts “This is loop #{i}”
 end

which is hard not to understand.

I had not heard of Chang before, but it’s a pleasure to read about this Rubyist from Singapore. PuneRuby is a blog covering Ruby+Railsish activities in Pune, India. One of its authors, Satish Talim has broadened his coverage to capture a slew of very nice interviews with some lesser-known but highly intriguing folks. It’s great, really, just great.

said on 23 Aug 2006 at 03:54
I’m not quite so sure what’s so beautiful about that. Looks like rampant fanboyism again. How about some Io for hard to not understand?
0 to(4) foreach(i, writeln("This is loop " .. i))
Don’t kill me?
said on 23 Aug 2006 at 05:24

Arrogant, can you explain the strange dots in that expression to the readers at home?

said on 23 Aug 2006 at 06:49

I don’t get Arrogant’s point. This blog is obviously about Ruby, so why would you complain about praise for this particular language in place like this? Also: Stating that Ruby is beautiful is non-exclusive. Io may as well be very easy to comprehend. Subjective opinion: I think the Ruby bit is easier on the eyes (less parens). Whatever suits you best.

said on 23 Aug 2006 at 07:14

Arrogant: Fanboyism? Is that something like hijacking a ruby thread to promote your own favorite language? otaku taku? :)

Also, where is your implementation of the Number#to method?

ExceptionId: Io.Object.doesNotRespond
Description: Number does not respond to message 'to'

You know that…

0.upto(4) { |i| print("Ruby is ", i, " times neater\n") }

:P (all in good humor)

Manfred: In Io ’..’ is the concatination operator.

said on 23 Aug 2006 at 09:22

Hmm, maybe Arrogant needed to pick a better example.

Reading both I think the Ruby version is a little more direct—“do X five times” versus IO’s “for each integer in the range 0 through 4 (inclusive) do X”. I mean yeah?

Granted, in the Ruby version it’s slightly less obvious where i is coming from, except that the context of its use makes it a bit clearer.

said on 23 Aug 2006 at 09:42

I was writing that response to the interview at 2am in the morning, and that was the only thing I could think of at the moment. Blushes on the not too good English.

said on 23 Aug 2006 at 11:36

I have yet to penetrate the deep mystery that is Hoodwink.d :-) When I get time I’d like to explore Io, it’s get-new-object-by-cloning is a style I’ve never used, so it would be interesting to experience that. It will have been chosen for a reason I don’t yet appreciate.

said on 23 Aug 2006 at 12:30

0 to(4) foreach(i, writeln(“This is loop ” .. i))

rofl

said on 23 Aug 2006 at 17:24

Granted, in the Ruby version it’s slightly less obvious where i is coming from, except that the context of its use makes it a bit clearer.

Fortunately, my proposal solves this problem.


5.times do HEY RUBY PLEASE GET ME MY VARIABLE i AND USE IT IN THIS BLOCK/CLOSURE THING
  puts "DAMN, IT'S #{i}" 
end

I expect this to be parsed forthwith.

said on 23 Aug 2006 at 17:33

Arrogant’s code is nasty, give me Ruby any day.

But I do hate the double-pipes.
said on 23 Aug 2006 at 17:55

The double pipes take some time to get used to, but I really see no other nice syntactical way to declare how variables are to be passed into the block.

said on 23 Aug 2006 at 18:13
Why not simply this?
5.times do i
said on 23 Aug 2006 at 18:18
Perl 6 has a somewhat reasonable syntax, the pointy block. Looks kind of like this:

for(0..4) -> $i {
  say "This is loop $i" 
}
Not that this is necessarily a good fit for ruby, but it does seem more consistent with the use of a block as an anonymous function (or method, whatever you want to call it)
said on 23 Aug 2006 at 19:17

I like the fieldgoals, personally. They syntactically distinguish block arguments from method arguments, and (to me at least) appear to be passive, thus recieving the arguments, whereas the arrow looks to me to be active, like it’s injecting something into the mix. But the arrow thing is mainly a learned/cultural convention…there could be some culture somewhere or in theory that sees the “pointy” end of the arrow as a collector and the “stem” end as doing the pointing so that it would mean to them the exact reverse of what I percieve it to mean. I still like the fieldgoals, though.

BTW , Sausheong, your English was fine. Keep up the good work or promoting ruby. :)

said on 23 Aug 2006 at 19:18

*or promoting = of promoting

said on 23 Aug 2006 at 20:23

I agree Ruby is very readable.

...but, the above “canonical example” of Ruby is somewhat misleading. Dont we Rubyist agree that it is good programming practice to typically write:
5.times { |i| puts “This is loop #{i}” }
When the expression fits on one line? :)

...and then one could ask, whats the { } for? :)

but I degress…I think has the cleanest syntax of the languages I currently use and it is a pleasure to work with.

-happy coding

said on 23 Aug 2006 at 22:17

I also hate Ruby’s #{} and =>, and :var rather than var, but other than that I like Ruby!

said on 24 Aug 2006 at 02:25

Well, sometimes it’s just enough to ask “Can your programming language do this?” ( http://www.joelonsoftware.com/items/2006/08/01.html ) !

said on 24 Aug 2006 at 08:33

Joe Ruby: You don’t have to use the string interpolation syntax; you could use the format syntax:

a, b  = 'string', 2
s1, s2 = 'This is a %s', 'A %s and a %d'
#these all do the same thing
#literal
p(s1 % a) 
p(s2 % [a, b])
#format
p(format(s1, a))
p(format(s2, a, b))
#sprintf (see also printf)
p(sprintf(s1, a))
p(sprintf(s2, a, b))

The => can be replaced by commas when using the literal or square-bracket constructor:

h = {"a", 100, "b", 200}
h = Hash["a", 100, "b", 200]

And the :symbol notation can be avoided by using only strings:

m = method('puts') # means the same as:
m = method(:puts)
#The string will get converted to a symbol
#internally if necessary (this isn't always
#true--e.g., hashes indexed by symbols--but 
#there is always "string".to_sym)

The || goalposts is all you’re really stuck with.

said on 24 Aug 2006 at 10:02

Hmm. In thinking about format and sprintf...wouldn’t the Ruby Way™ be to add those methods to String...something like:

class String
    def format(*args); Kernel.format(self, *args); end
    alias_method(:sprintf, :format)
  end

Then you’d do:

'I like %s'.format('ruby')

And that also makes me wonder about top-level print functions from Kernel also…shouldn’t it be:

'Fire on the mount'n, run boys, run'.puts

Kind of strange since I’m used to top-level print functions…but what say ye, oh austere rubyists of the red hand?

said on 25 Aug 2006 at 13:15
MonkeeSage:
"I like %s".%('ruby')
"Fire on the mount'n, run boys, run".display(STDOUT)
STDOUT.print("Fire on the mount'n, run boys, run")
(STDOUT is the default for #display)
said on 25 Aug 2006 at 15:18

I was suggesting that format and sprintf act like the “literal” with modulo. ;) I figured that p, puts and print could be added to Object and format, sprintf and printf could be added to String. It works. I wasn’t aware of Object#display! Learn somthin’ new every day.

11 Jul 2010 at 21:22

* do fancy stuff in your comment.

PREVIEW PANE