4 ms·
fun1 x fun1 x + y fun1 x + y.foo fun1 x + y.foo bar What will happen in this perfectly valid Ruby code? Where do the parentheses belong? Do you know o
by inklesspen 18y ago
fun1 x
fun1 x + y
fun1 x + y.foo
fun1 x + y.foo bar
What will happen in this perfectly valid Ruby code? Where do the parentheses belong? Do you know off the top of your head?
While any programmer who would write this deserves to be fired, I can't come up with a good reason why it should be valid to write it in the first place.
- shevy 18y agoLooks contrived, because I dont know many ruby users who would write fun1 for a method. Why? Because in ruby there are methods. You dont find many people doing "fun" for a method - the intent clearly was a "function". But where are they? ;) Anyway, here is the one that comes to my brain flow naturally. It seems python writers need () in order to feel happy, otherwise they think they get confused about things (hopefully they dont have a small brain): fun1 x fun1 x + y fun1 x + y.foo fun1 x + y.foo bar What will happen in this perfectly valid Ruby code? Where do the parentheses belong? Do you know off the top of your head? fun1(x) fun1(x + y) fun1(x + y.foo()) fun1(x + y.foo(bar)) Btw you omitted what x and y are. My first assumption is that these must be variables that allow the + method It does not really make a lot of sense though, give the last example: class Cat def initialize(name) @name = name end def foo(how = :h) case how when :h how = 'happily' when :u how = 'unhappily' end puts @name+' meows '+how+'.' end end y = Cat.new 'Tom' y.foo # "Tom meows happily." bar = :u y.foo bar # "Tom meows unhappily." x = '"At the end of the day " x + y.foo bar # "At the end of the day Tom meows unhappily." etc.. I simply have no idea what fun1 should do. Maybe it will involve Jerry mouse and return a conditional story where it is explained why Tom the cat is unhappy. Btw this is a really contrived example because I tried to model your stipulation of the above code into this. It really makes no sense at all to use variables without any real idea why one should use that. Why do people WANT to be complex when simplicity is so much more elegant?
- shevy 18y agoThis comment field is not very good, it killed my newlines! :( I demand the pre tag or a "code" tag or something that allows us to add code nicely formatted :P
- Xichekolas 18y agoAnything indented two spaces acts like it's inside pre tags. Like this. And This. Etc.
- jamesgolick 18y agoYes, I do know where the parentheses belong. Like I said earlier, it isn't hard to read ruby like this once you get used to reading ruby. > While any programmer who would write this deserves to be fired... That's exactly my point. The only example you could come up with was a straw man. > ...I can't come up with a good reason why it should be valid to write it in the first place. Can you come up with a good reason it matters in practical, real world situations?