Live data from Hacker News

“Did you mean?” Experience in Ruby

yukinishijima.net

81–90 of 187 posts

Re: “Did you mean?” Experience in Ruby

#81
post #77
post #40

A lot of people are recommending IDE-like tooling--but in truly dynamic language (one with a "living image" with path-dependent monkey-patched behavior that can't be replicated during static analysis, like Smalltalk--or, sometimes, Ruby) there's a more idiomatic way. In a dynamic language, if you're at all unsure of what code you need to write, then you don't write it in your editor in the first place. Instead, you b…

And with Ruby, especially since you mention Smalltalk, we have a tool that is getting closer to the kind of live introspection and modification that Smalltalk is famous for: Pry [1]. Pry lets you call "binding.pry" anywhere in your program to dump you into a shell within that context, with full access to local variables etc.. And tab-completion and plenty of introspection features. I frequently find myself triggering…

I second Pry. It's amazing how fast you can use AR to add a little add-hoc business logic on-top of a DB and then drop a console with Pry to get a sort of custom interactive command line DSL :)

TBH if I was writing anything large enough for this kind of stuff to get out of hand, I would seriously consider a statically typed language like Golang or C#/F#(which both have absolutely fantastic tooling). REPL's don't help the next person to come along figure out what your methods are taking in and what those calls return. But I digress.

I don't always cut Ruby, but when I do I cut it with Pry... Yeah.

Re: “Did you mean?” Experience in Ruby

#82
post #76

Earlier quoted context omitted.

It's not a problem that can be solved for a language like ruby without adding type hinting (and probably interfaces) to the language. FWIW php actually does support this.

Which becomes ironic because you end up writing way more text into the file for phpdoc then you would have if it were a static language to begin with; and you end up treating the variables as static type anyway! :|

PHP5 has real type hinting in the language, so you don't need to write any more than you would in Java.

Re: “Did you mean?” Experience in Ruby

#83

Obviously this is a useful tool, and all the power to the author for finding what looks to be an excellent solution, but this line bothers me: >>> Sometimes I wasted hours and hours just because there is one character difference. I hate it. This shouldn't happen. Ever. This should not be a problem anymore. These are the sort of errors that we can catch immediately and should be caught immediately. From looking at the…

While you can catch some of these, for a language like Ruby, it's an incredibly hard problem to catch all of these errors immediately because the methods that can be called on a given named entity can not in the general case be known by statically inspecting the source, and you can not in general safely instantiate the application because even class definitions are executed and can have side effects, and the methods available can even vary based on environmental factors, such as whether or not you get a database connection, and what's in the database.

Consider that it is a common pattern for Ruby ORM's to either use method_missing or dynamically define methods to correspond to the current (at connection time) set of columns present in your database.

And "thanks" to the ability to monkey patch and redefining methods, even determining if something "obviously" safe like 42.to_s is not.

There's no way for your editor to handle that unless you stand up a version of your app with an instrumented language environment and lets the editor poke around. Now that is possible with tools like Pry, etc., but it takes a lot more work to do safely (because your editor can't know if it can safely start your app).

Re: “Did you mean?” Experience in Ruby

#84
post #71

I've been pairing with ruby / rails developers since 2010 and coming from statically typed languages, it's unbelievable how much time gets spent playing "guess the method name". Even in rich IDEs like RubyMine, the utter lack of context in any given file in rails leaves programmers typing their best guess of a method name, running the tests, rinse, repeat. This solution, while creative and laudable, solves a problem…

I have exactly this frustration as a person coming from static languages (mostly C++) to web dev. Anybody can recommend something more comfortable? I like the concept of Vaadin, but I'd prefer something non-Java.

If you're coming from mostly static languages, then this is a good choice for web dev: https://golang.org/doc/articles/wiki/

Re: “Did you mean?” Experience in Ruby

#85
post #71

I've been pairing with ruby / rails developers since 2010 and coming from statically typed languages, it's unbelievable how much time gets spent playing "guess the method name". Even in rich IDEs like RubyMine, the utter lack of context in any given file in rails leaves programmers typing their best guess of a method name, running the tests, rinse, repeat. This solution, while creative and laudable, solves a problem…

I have exactly this frustration as a person coming from static languages (mostly C++) to web dev. Anybody can recommend something more comfortable? I like the concept of Vaadin, but I'd prefer something non-Java.

Try Go. It is a lot like C and compiles to concurrent machine code. It is also very easy to learn and use.

Re: “Did you mean?” Experience in Ruby

#87
post #76

Earlier quoted context omitted.

Which becomes ironic because you end up writing way more text into the file for phpdoc then you would have if it were a static language to begin with; and you end up treating the variables as static type anyway! :|

PHP5 has real type hinting in the language, so you don't need to write any more than you would in Java.

phpdoc handles much more than just typing function parameters.

Re: “Did you mean?” Experience in Ruby

#88
post #31
post #24

Earlier quoted context omitted.

Ruby has a linters, but this sort of problem is hard to solve with a linter in a dynamically typed language. It's even harder in a Rails environment since Rails adds many methods to your classes at run time.

Yeah, since it's a dynamically typed lang with inheritance and late binding, doesn't performing static analysis on Ruby code to determine whether a method is defined or not, basically require a solution to the halting problem? I'm under the impression that you have to actually execute Ruby code to find out with certainty where the methods it calls are defined.

You can solve the "90%" problem statically. My Ruby-compiler-in-progress warns of methods that have not been seen, and I may eventually add some (entirely optional) pragma to allow hinting to reduce false positives (though getting that non-intrusive will be a fun challenge).

But you are right, the general case does require a solution to the halting problem, something which is trivial to demonstrate very explicitly:

    eval(STDIN.gets)
    42.will_i_halt?
Put that in a file, and run it. Press enter, and it halts. Or cut and paste something like this, and it doesn't:

    class Fixnum; def will_i_halt?; loop {}; end; end
And consider that even "require" is a practical equivalent to "eval", so trying to look for eval() calls and similar constructs and think you're safe doesn't work, unless you have a guarantee that the interpreter load paths will be the same when the code is run as what you think it'll be.

But in more pragmatic terms, this is a real issue because many Ruby ORMs for example will add methods that depends on the current state of the databases they connect to, so many typical Ruby applications will actually validly use method names that are not explicitly defined anywhere.

Re: “Did you mean?” Experience in Ruby

#89
post #71

Earlier quoted context omitted.

I have exactly this frustration as a person coming from static languages (mostly C++) to web dev. Anybody can recommend something more comfortable? I like the concept of Vaadin, but I'd prefer something non-Java.

Try Go. It is a lot like C and compiles to concurrent machine code. It is also very easy to learn and use.

Er, you mean to say "compiles to machine code." There is no such thing as concurrent machine code... Now the Go language has some fantastic data structures baked-into the syntax that provide a language with which to construct concurrent programs.

Re: “Did you mean?” Experience in Ruby

#90
post #8

I would love it if instead of quitting my program with an error, it just went ahead and called the method it thinks I'm referring to. This would remove a lot of unneeded friction from web development.

The person taking over your projects when you move on would put a bounty on your head if you were to do that.
Post reply on HN