#ruby - 06 May 2018
« Back 1 day Forward 1 day »
[00:22:18] luminousnine: has left #ruby: ("Kicked by @appservice-irc:matrix.org : removing from IRC because user idle on matrix for 30+ days")
[00:32:59] luna_x[m]: has left #ruby: ("Kicked by @appservice-irc:matrix.org : removing from IRC because user idle on matrix for 30+ days")
[01:24:50] hays: is it fair to say that if i want a singleton, I should probably just have a class with class methods?
[01:53:07] hays: im just trying to wrap my head around classes being objects also, and how that might be actually used
[01:56:38] garyserj: baweaver: here is a case where I prefer goto, https://repl.it/repls/DarkorchidWaryHashmap
[01:56:59] hays: i guess one thing nice about using the Singleton module is that you can make something a singleton or not pretty easily, whereas if you make things classes and class methods, there's going to be more refactoring
[01:58:10] garyserj: I don't want to exit the whole program. e.g. say i'm running sinatra web server I don't want the server to shut down
[01:58:56] baweaver: so to superimpose it into a language which has these concepts is not a great idea
[01:59:18] garyserj: well as I showed, I managed without a goto, by using an extra nesting of if statement
[01:59:36] garyserj: Gotos can be used to implement many things.. they can be used as an alternative to methods.
[02:00:54] garyserj: which states quite rightly that no more should an entire word be considered a bad thing.. like banning a word from a language.
[02:01:19] baweaver: and you're abusing it in a language which has features which invalidate its use
[02:01:21] hays: here is the thing--most people work with other people, so it 99% of the world doesn't want to use goto, its probably a bad idea unless there is a large benefit
[02:01:38] garyserj: well I didn't use it, I used an extra nesting of if, though it is a bit uglier.
[02:02:11] hays: even Dijkstra's original paper talked about ways to use goto that were valid, but argued that developers tended to use it in ways that led to problems
[02:02:42] garyserj: yeah and somebody that knows about the ugly uses of goto can use it in an ok manner.
[02:04:16] baweaver: It's not even really a discussion. You provided a sample which doesn't actually show your usecase
[02:04:31] hays: i prefer the below, although if A()==true was cheap, I'd consider just flattening it a bit and just having 3 cases
[02:04:51] baweaver: we assert that there are language features that make the usage of such completely unnecessary
[02:05:36] baweaver: You're likely using it for flow control in a single action of a Sinatra controller of sorts.
[02:06:16] baweaver: Most conditional logic can be refined further into classes and modules for easier testing and reuse
[02:06:28] baweaver: and more than likely the reason you would have to do heavy conditional checks is that you don't really have a RESTfully oriented routing setup
[02:07:10] hays: baweaver: i remember seeeing examples of that in Strousoups old C++ book and I didn't really get it
[02:07:35] baweaver: This cascades into multiple levels of design concern in which the thing that doesn't make sense isn't the dislike of goto, but the dislike of patterns that have long since made its usage a crutch
[02:07:43] hays: how might one eliminate conditional logic in the simple case we're talking about here
[02:08:36] baweaver: You cannot derive the concern of the application from names like A, B, yyy, and zzz
[02:08:54] baweaver: So using them to make an assertion about the validity of goto is an architectural sham.
[02:09:21] garyserj: it's not to do with testing something that would or could be in the URL anyway.
[02:10:07] hays: garyserj: it might. but I actually think the structured version is cleaner than using goto
[02:10:16] baweaver: Are they routed objects based on params, are they queries, url segments, child objects? It could be a lot of things.
[02:10:27] garyserj: hays: with three ifs, I think so. With the extra nesting I don't think so.. though we disagree there with the latter.
[02:11:07] ruby[bot]: it seems like you are asking for a specific solution to a problem, instead of asking about your problem. This often leads to bad solutions and increases frustration for you and those trying to help you. More: http://meta.stackexchange.com/a/66378
[02:11:38] baweaver: You have a solution you want, and are fixated on it to the detriment of all possible better solutions.
[02:12:23] garyserj: baweaver: you accused me of trolling as if i'm trying to provoke this conversation with you about goto further but i'm not.
[02:13:23] baweaver: It's because it's so unnecessary in the language that I'm trying to warn you of it
[02:14:26] hays: baweaver: I've definitely struggled with complex/nested logic when doing state machine kind of stuff for various logic controllers. sometimes its hard to avoid things looking a bit spaghetti like
[02:15:07] garyserj: hays: spaghetti like and nested ifs are very different things.. nested ifs, while they can be ugly, are technically not spaghetti code.
[02:15:29] baweaver: This is a good read on the subject - http://raganwald.com/2018/02/23/forde.html
[02:15:39] garyserj: no gotos and it may still be ugly though technically not spaghetti code, just ugly code ;-)
[02:16:25] baweaver: It means your object or method knows too much and should likely be broken down more.
[02:19:05] garyserj: you could flatten nested ifs into multiple ones with longer conditionals.. How are you solving that with more methods?
[02:19:09] hays: the machine is suppose to do x,y,z except there's this global override. and this other thing. and oh yeah if there's a fire than this changes.. etc.
[02:20:16] baweaver: and that's the dig, you cannot abstractly capture that level of nuance with fake names and short examples.
[02:21:47] baweaver: 100 line methods, imperative scripts, all of it: how do you test each component piece that makes an application? You can't without a lot of mess.
[02:22:02] garyserj: well, iirc the example I had was.. get '/' do then check params[:g].has_key?(:tempfile) (that was condition A). if it does then test if the file ends in .htm or .html. (That was condition B).
[02:22:17] baweaver: and once you get into larger applications that are customer facing or mission critical?
[02:22:37] garyserj: baweaver: nobody is suggesting not using methods and nobody is suggeseting having 100 lines of code not broken into a different method.
[02:24:00] hays: and if you told customers you were writing their product in ruby they'd probably fire you and hire someone to put it into a terrible thing like this https://selinc.com/products/3555/
[02:25:38] baweaver: Especially around SCADA and controllers in general, then even further back into RPG and COBOL era
[02:26:58] hays: yep, some coke addict wrote the code 20 years ago, no one has a copy, and if you go and extract it you are not ging to like what you see
[02:27:37] baweaver: Thing is, we've moved on as an industry from a lot of it to a lot more testable interfaces.
[02:29:02] baweaver: Now logic around handling how to write something given suffixes, that should likely be a handler of some sort meant to handle how a route responds to something
[02:29:15] baweaver: param wise, unless it's an index with a search action that's a hint you need more routes.
[02:29:46] baweaver: We had one recently where someone loaded everything into '/' using params for dispatch, that's not ok.
[02:30:17] baweaver: Turns out a lot of the actions had to do with solid concepts like hosts, databases, and other definitive objects you're performing actions against
[02:31:49] garyserj: baweaver: that doesn't make sense. in my case I have a form, they select a file and click submit. It makes a request, to one route.
[02:32:16] baweaver: A lot of it is that things just do too much and become impossible to test or reason about after a while
[02:33:19] garyserj: And now that you know more about it, can you tell me how my simplified example that I gave you, changes whatever solution you haven't come up with?
[02:34:09] garyserj: you were incorrect when you said you could solve it with more methods, and incorrect when you said you could solve it with more routes. So now you know more, how would you solve it?
[02:34:36] baweaver: garyserj: I accuse you of trolling because you come with an incredibly generic solution to make a point, and once I start trying to go around potentially related concerns you start going even further in on it.
[02:34:37] garyserj: I think what hays said, of flattening the ifs, is perfectly good. And totally based on my simplified example.
[02:35:53] garyserj: and you still accuse me of trolling? I only continued the discussion with you because you kept requesting that I continue it.
[02:38:36] hays: another way to avoid the goto is to have the block of code in function and use early returns. that can get ugly too though
[02:54:20] hays: garyserj: dijkstra seems to have blown frank rubin's response out of the water https://www.cs.utexas.edu/users/EWD/transcriptions/EWD10xx/EWD1009.html
[02:55:32] hays: i think baweaver was trying to say that well designed software interfaces and modern programming language features completely eliminate the temptation of using goto, but i might be micharacterizing it
[02:56:16] baweaver: Pretty much, then laying out normal concerns people tend to reach for it in and how to get around it.
[03:53:41] garyserj: somebody might make a response e.g. there is a paper called "goto statements considered harmful, considered harmful"
[06:25:39] konsolebox: hays: he advocates the use of an extra bool variable (which by concept implies an instruction for assignment, and another for comparison). how does that avoid extra memory or cpu cycle vs. a single goto to a common set of instructions? note that i'm not giving my general opinion of which practice is better yet. but the idea of this guy that everyone hypes about seems to be common sense.
[10:57:32] hays: konsolebox: point taken, there is probably a way to optimize out the bool, although I think that is unrelated to the use of goto but rather from interring the bool from the loop variable value
[11:40:42] Mike11: hello all, I have a frozen String str and I want to get a new copy of it after replacing a range of characters within it
[11:46:14] Mike11: so, my question is: is there a method that acts like String# but returns a new string instead of mutating the current instance?
[12:04:04] konsolebox: hays: it would be interesting to get his idea about jumping out of multiple loops. common reply would be encapsulate code inside functions. but that doesn't always work right, and it doesn't always make things simpler. thankfully Ruby has throw and catch, but that's no longer about programming in general, and is about language features which was one he is suggesting not to focus to.
[12:20:27] hahahahah: hei guys... i never programmed ruby on ma life.. and I need something like this: https://stackoverflow.com/questions/5424354/regex-to-remove-non-letters#5424382 for logstash to convert a url to a action...
[12:20:47] Mike11: konsolebox: seems like there is no one liner for my function. kind of strange that all String functions have mutating and pure versions, while String# is an exception
[12:21:13] hahahahah: example: /directory/sub/action.php?var=1&var3=dsgadsa should result in: /directory/sub/action.php how can I test my code? is there a ruby console like python?
[12:22:21] Mike11: I just want to return a new String that contains the same content of a frozen string after replacing a range
[12:24:15] Mike11: konsolebox: seems like the most brief way, no problem, thank you for your help :)
[12:24:56] hahahahah: '/directory/sub/action.php?var=1&var3=dsgadsa '.gsub(/\A([^\?].*)/, '') => "" (i guess is not gsub... :o )
[12:26:42] hahahahah: '/directory/sub/action.php?var=1&var3=dsgadsa'.split('?', 0) => "/directory/sub/action.php" works \o/
[12:43:01] konsolebox: hahahahah: there's no easy way to do that unfortunately. you have to have a solution that can parse php code. `split(';')` would also split semicolons that are no longer part of the syntax scope.
[12:49:35] konsolebox: hahahahah: try to give an example then, and elaborate what your target goal is
[12:52:25] hahahahah: Ruby exception occurred: undefined method `split' for nil:NilClass how can i avoid this? :o or check if the string exists?
[12:57:15] hahahahah: konsolebox: nevermind.. :| THIS is the solution -> code => 'event.set("response_time_ms", event.get("response_time").to_f * 0.001); event.set("action", event.get("request").to_s.split("?", 0))'
[12:58:10] konsolebox: hahahahah: looks like you have to check if get("request") returns a string or not
[14:16:16] garyserj: hays: that link just has dijkstra making his case that frank rubin is a bad programmer. IF you look at the example in the second answer here https://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto It is *Exactly* the usage I used.. which is described as a good use of Goto.
[14:29:10] garyserj: hah, linus should've written the goto statements considered harmful considered harmful paper!
[15:21:51] havenwood: garyserj: Ruby actually has GOTO. You just have to enable the SUPPORT_JOKE flag when you compile.
[15:23:33] havenwood: garyserj: https://github.com/ruby/ruby/blob/14a3499c4f9dfc9ee102cbed5fe37481282f5f5a/compile.c#L6387-L6432
[17:44:17] RedNifre: Is there a way to chunk an array, e.g. [1,2,3,4,5].chunk(2) returns [[1,2],[3,4],] and possibly even [1,2,3,4,5].chunk(2,0) -> [[1,2],[3,4],[5,0]] ?