Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ruby face palm
#1
Been going through this book for kicks:  
Mazes for programmers

The book is written in Ruby and I figured, "Hey, why not just pick up Ruby," while I went through it (even though translating the code to python is fairly trivial).

Despite the constant end tags which I think make everything ugly as hell I just ran into this loveliness.

The following is essentially looping over a range in python and assigning each value to the name index; straight forward:  

0.upto(path.length - 2) do |index| A little weird but fair enough.
0.upto(path.length-2) do |index| Yeah, that seems fine too.
0.upto(path.length -2) do |index|
Error:
`length': wrong number of arguments (given 1, expected 0) (ArgumentError)
... wtf.

Not that I condone the latter's operator spacing, but the fact it means something different is insane.
Reply
#2
It's because parenthesis are optional for calling methods in ruby, so for times like this, it isn't obvious whether you're passing arguments to path.length(), or if you're operating on it's result. You can add parenthesis to all three of those, and they'll all do the same thing.

Also, the do loop takes a function, the |x| syntax is ruby's version of lambda. It's been a while since I've ruby'd, but I'd be willing to be the do had an "end" afterward, with some sort of function body that used the index.

Ruby's an alright language.
Reply
#3
(May-09-2017, 09:42 PM)nilamo Wrote: It's because parenthesis are optional for calling methods in ruby
Don't get me wrong.  I know exactly why and what is happening.  I just think it is a terrible design decision.

The fact that function(arg1, arg2) and function (arg1, arg2) are different is awful.  Not to mention the implicit function calling without requiring parenthesis means that functions aren't first class.  In order to pass them as arguments or put them in containers you need to do silly stuff like wrap them in classes.

Quote:Also, the do loop takes a function, the |x| syntax is ruby's version of lambda. It's been a while since I've ruby'd, but I'd be willing to be the do had an "end" afterward, with some sort of function body that used the index.
Yes, there is obviously a block that uses the index variable I didn't include.  This was just the context in which I hit the problem and had trouble finding where the problem was.


Quote:Ruby's an alright language.
I like parts of it but there are definitely some design decisions I think are questionable as hell.
Reply
#4
(May-09-2017, 09:42 PM)nilamo Wrote: Ruby's an alright language.
https://www.destroyallsoftware.com/talks/wat
Reply
#5
I'll just leave this here: https://blog.daftcode.pl/fixing-unicode-...d7f6377388
Reply
#6
(May-09-2017, 10:17 PM)micseydel Wrote:
(May-09-2017, 09:42 PM)nilamo Wrote: Ruby's an alright language.
https://www.destroyallsoftware.com/talks/wat

JavaScript takes a more serious beating. Hilarious all along!
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#7
Weak typing seems takes the most serious beating (note: Python 3 became more strongly typed <3). But yeah Ruby and Javascript have issues. Javascript you kinda have to live with right now but Ruby... just why when you have Python?!
Reply
#8
(May-11-2017, 09:41 PM)micseydel Wrote: Javascript you kinda have to live with right now
Typescript is actually really cool.

Instead of a lot of other languages that transcompile into javascript, typescript is just javascript, but with types added in. Valid javascript is valid typescript. You can also write valid ES6 javascript (let expressions, anonymous functions using => syntax), and compile them into valid ES3 javascript, which can run on ancient browsers like IE6. So using things like modules, classes, private methods, are all possible, without giving up browser share.

The downside, of coarse, being that you suddenly have a stunning number of technologies to learn all at once. "Get Typescript, write typescript. Get gulp, create a gulp file to compile your typescript and move the compiled javascript into a 'build' directory. Then get Browserify, configure Gulp to feed the compiled javascript into Browserify to create a single javascript file called 'bundle.js', and THAT'S what you actually link to in your html." I almost quit learning typescript when I saw that that was what is involved just to get started.
Reply
#9
Yeah, reminds me of https://hackernoon.com/how-it-feels-to-l...a717dd577f
I get that alternatives to Javascript are around, and that they might even be kind of standard (for the moment) but I'm disconnected enough from that to just think of Javascript as a necessary evil... for now :)
Reply
#10
(May-11-2017, 10:23 PM)micseydel Wrote: Yeah, reminds me of https://hackernoon.com/how-it-feels-to-l...a717dd577f
Heh, I was just reading through that yesterday.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020