#ruby - 11 June 2019
« Back 1 day Forward 1 day »
[04:48:06] syndikate: One of the codebase that am working on has a case statement where the `when` condition has multiple (more than 5) values against it. What is a better way to write this?
[07:12:51] baweaver: syndikate: If you share some of the code we'd be a lot more able to tell you if a better solution exists.
[07:14:32] mcspud: https://medium.com/@baweaver/ruby-2-7-pattern-matching-first-impressions-cdb93c6246e6
[07:15:15] syndikate: Hey baweaver, are you this person -> https://www.youtube.com/watch?v=tmJlos2CST4
[07:16:15] syndikate: Haha nice. Good to know :D I was watching that video yesterday, gotta finish it
[07:17:00] baweaver: Also have another talk accepted to Southeast Ruby to basically go through as much black magic as I can fit in 40m
[07:17:00] syndikate: Yes, I think I didn't finish that too, not because I didn't like it coz I haven't had much time recently
[07:18:35] baweaver: It even has a quick intro chapter to it: https://gist.github.com/baweaver/1ae6761dbc121a860597acdf053433f8
[07:18:41] syndikate: I really like the way how you put so much effort, like literally making a play :D
[07:19:30] ryez: `puts(Integer(ARGV) rescue 3)` causes a syntax error, but works with an extra space after `puts`, or an extra pair of parenthesis around the argument of `puts`, do you think this is a bug?
[07:22:05] dminuoso: ryez: Most of it is just iterative retrofitting features without much design. If you ask Matz & Co. they'll probably even tell you it's a feature.
[07:23:43] baweaver: dminuoso: Na, just screwing with ya. Got your hopes up though, no? How many pages of Google did you just get through?
[07:25:23] ryez: dminuoso: got it, nice feature, so I need to shut up instead of yelling loud with a bug report :-(
[07:25:54] syndikate: How do you pick up so many topics? I have been trying to much figure about something to talk at local meetups
[07:30:16] syndikate: I had read your post on pattern matching although it didn't feel natural to me initially, I will give it a go again.
[07:30:40] dminuoso: baweaver: Ive went deep enough into type theory and language design to understand why retrofitting a type system is a really bad idea in general.
[07:32:05] dminuoso: baweaver: In general you should start out with a type system, include all properties and feautres you want into it - and then carefully design the language around the type system.
[07:33:15] dminuoso: syndikate: In principle its the idea of being able to "add" and "multiply" types together.
[07:33:39] baweaver: That's pretty accurate, as Haskell leans heavily on them and Category Theory for abstractions
[07:34:06] syndikate: dminuoso, Oh. Okay. What all did you go through to learn more about these stuff?
[07:34:39] baweaver: Functor and Monoid are about as common as anything in Haskell, though to be fair in most any languages
[07:34:42] dminuoso: syndikate: Pierce's book Types and Programming Languages is probably the best book to read.
[07:35:48] dminuoso: syndikate: To give you a quick idea of what ADTs are, it's the idea of being able to combine "union" and "struct" together in a nice fashion
[07:36:13] dminuoso: syndikate: So for example you might have the need to express `this thing is either an Int or a String`, or you might want to say "this thing has both an Int and a String"
[07:37:21] baweaver: What's funny is Reducing Enumerable is literally an introduction to Monoids without ever mentioning them
[07:38:00] baweaver: This was a fairly good overview - http://www.tomharding.me/2017/03/03/fantas-eel-and-specification/
[07:40:53] baweaver: Those two properties are called Identity (empty) and Closure (binary operation to join, returns same type)
[07:41:20] baweaver: Add one more property, Associativity, and you have a Monoid. Associativity is being able to group them as long as the order stays the same
[07:43:10] baweaver: Granted that Integer addition has a few more properties to it that make it into something entirely different called an Abelian Group
[07:44:25] dminuoso: ACTION is not convinced that free monoids are the best way to introduce what a monoid is..
[07:44:25] dminuoso: syndikate: The term Monoid captures the raw idea of "combining two things together into one" in a way that makes sense. Integer multiplication, integer addition, array concatenation, string concatenation, picking the lesser of two natural numbres..
[07:45:23] dminuoso: syndikate: Anything more is just overwhelming and requires studying many examples in a setting where you can actually make use of what monoid captures
[07:45:32] syndikate: baweaver, Thanks a lot! Man those re some good stuff in simple words. Saved it
[07:46:19] dminuoso: syndikate: The monoid abstraction does not hold value in languages that dont employ polymorphism.
[07:46:29] baweaver: Mostly recognizing patterns. If data behaves a certain way we can work with it in predefined methods
[07:46:43] dminuoso: syndikate: For example my API might say "Im gonna squish things together, but *you* decide using what monoid"
[07:47:05] dminuoso: syndikate: In Ruby you would generally encode this by returning a list (such that the consumer of your API can squish it together)
[07:48:05] dminuoso: syndikate: Its very general and not easy to spot unless you are in the habit of writing highly polymorphic code.
[07:48:52] dminuoso: And in languages that are very non-principled like Ruby it falls extremely short.
[08:00:06] dminuoso: So you'd start by thinking of a log as just string concatenation. You concatenate new entries onto the existing log.
[08:00:28] dminuoso: And if, for symmetry reasons, you were forced to emit log entries even if you didn't have anything to say you could still emit an empty string.
[08:00:45] dminuoso: Which is fine, because concatenating an empty string to some string A yields that same string A back.
[08:01:45] dminuoso: But perhaps we are not interested in a string log. Perhaps our log is just a "number of actions taken". So instead of emitting strings that get concatenating, you could emit numbers that get added.
[08:02:28] dminuoso: Now remember since we said, we might be forced to emit a log entry even if no action is taken. We could emit 0 in those cases, which is fine because adding 0 to a number does not change it.
[08:04:46] dminuoso: Or perhaps you have some software processing personal data, and the log is keeping track of the "oldest person" seen. If at some point you are not processing any person but were forced to produce a log entry, you could produce "0 years".
[08:09:02] dminuoso: syndikate: Now you might begin to see that in all cases you are somehow "combining/smashing" 2 into 1, and you always have a special element you can throw into the binary operation to produce a noop
[08:11:47] syndikate: A noob doubt here - so if am writing a moonoid function it would have a local value inside (closure like) which has the default starting value?
[08:18:33] dminuoso: syndikate: we could consider a module called Monoid, sort of like an interface, that you can include into a class.
[08:19:25] dminuoso: syndikate: One would be `def mappend(other); ... implement here; end` and the other would be `def mempty; ... implement here; end`
[08:20:58] syndikate: Okay gotcha, you are talking as monoid as on object, I was talking monoid as function
[09:24:48] DaniG2k: hello all. I'm trying to set up webpacker in my Rails 4 app. However, I cant seem to get it working with CircleCI
[09:25:06] DaniG2k: I've explained the actual error in this SO thread: https://stackoverflow.com/questions/55570569/rails-4-getting-webpacker-to-work-with-circleci/56521522
[09:26:11] DaniG2k: but basically the error I'm seeing in CircleCI is: ActionView::Template::Error: Webpacker can't find application in /home/circleci/octopi/public/packs-test/manifest.json
[10:58:25] xco: well then i’m confused. what i want to achieve is, i want to return, but before return do_something
[11:00:58] phaul: tap can be used for that, but also "and" is inherited from perl for the use case you described
[11:02:25] leftylink: I seem not to understand though. if I want to do_something and then return, then it'd be `do_something\nreturn`. so there must be some context I am missing
[11:03:09] xco: `do_something\nreturn` makes sense but i find it a bit weird to have the return all by itself like that
[11:04:48] phaul: so what's there under the link is that a = true and false is (a = true) and false whereas a = true && false is a = (true && false)
[11:05:21] leftylink: but I will be sad as a reader of the code if I have to look for `return` in places where I not expecting it
[11:06:18] phaul: I have never written code in ruby with and myself. but my undesrtanding of the intention behind it is this flow control
[11:07:56] leftylink: I must be frustrating, making alternative suggestions instead of answer the question. I will recuse myself
[11:10:02] leftylink: oh hmm, I realised, the fact that my suggestion of newline was accepted without complaint implies that the returning may not have been conditional on do_something. making it conditional is when I would have considered using some of the original questioned things
[11:19:05] phaul: that doesn't depend on the truthiness of the do_something whereas the versions with and do
[11:22:49] xco: phaul: this is what i’m talking about https://gist.github.com/xcobar/23d919440b2779cf10c7c88a87a0076f
[11:23:10] xco: i want something? to be check first, once it’s satisfied, the whole method should stop
[11:24:23] phaul: ok. so I think the way it's been done there is the best, because you don't care about the return value of do_something
[14:44:52] franklinyu: Hi everyone. I raised a bug https://bugs.ruby-lang.org/issues/15665 but it was changed to "Feature". How to change it back to bug?
[14:46:02] franklinyu: It is labeled as feature so it will only be in 2.7 branch, but I want it to be in 2.6 branch as well.
[14:58:08] havenwood: franklinyu: Your comment should be sufficient for nobu to get the heads up. I might ping again on that thread after a week or so.
[16:49:36] AndreYuhai: Hello there, I have a script and I want it to run everyday. And the script needs user input. Can I do it without using cronjob or sleep?
[16:54:43] havenwood: AndreYuhai: Why don't you want to use a cronjob or sleep? How are users providing input?
[16:58:27] havenwood: AndreYuhai: For periodic timers in general, check out the timers gem: https://github.com/socketry/timers
[17:00:54] AndreYuhai: havenwood: Well, I don't know if there is any pros or cons to sleep so I wanted to ask whether there are other ways to do it. The input would either be two dates or one date (that day's date). So I also do not know whether I can also get the date automatically for the cronjob. But I am searching and I will also check that gem. Thank you :)
[17:04:28] havenwood: AndreYuhai: The task itself takes some time, so sleeping 24 hours will always sleep a little later each day.
[17:06:15] AndreYuhai: havenwood: But would it still do that if we specify an exact date? Instead of saying 24 hours. I mean by checking the actual date and etc.
[17:40:08] havenwood: AndreYuhai: Or you mean you're sleeping for a shorter duration, and checking the date?
[18:19:44] AndreYuhai: havenwood: Yes, for example sleeping for a period of time and checking the date constantly, for example sleeping for a few hours instead of 24. Sorry for the delay by the way.
[18:23:45] havenwood: AndreYuhai: You can run a cronjob daily, and each day have it check the day. Alternatively you can user the timers gem and do the same.
[18:25:41] AndreYuhai: havenwood: Oh that is a good idea as well. I guess I will go with the cronjob but I will also check the timer gem. Thank you. By the way would there be any consequence of using 'sleep' on the server side compared to cronjobs?
[18:27:14] havenwood: AndreYuhai: With sleep, the time it runs just slips over and over. You need to do at least twice a day, to make sure it doesn't line up and slip so it doesn't skip a day. The timers gem keeps a separate thread so it can track the time without slipping.
[18:28:26] havenwood: AndreYuhai: If you don't want to have time slip between sleeps, the timers gem handles that for you.
[18:28:44] AndreYuhai: havenwood: Okay, thanks for the help. :) I will also check the timers gem and then decide which one to use.
[20:24:47] sputnik13: havenwood: thanks, I guess it's an app thing then, I'll explore further, thanks again
[20:57:24] theRoUS: I'm upgrading an old laptop; right now it's at Fedora 29. The ruby `gem` command is varfing as shown here: https://pastebin.com/Kjv263aB
[20:57:24] ruby[bot]: theRoUS: we in #ruby do not like pastebin.com, it loads slowly for most, has ads which are distracting and has terrible formatting. Please use https://gist.github.com
[21:10:02] theRoUS: phaul: maybe pastebin is a pain to read for some, but gist is (IMHO) a royal PITA to paste *to*. https://gist.github.com/RoUS/3c4ca47a971ed0bd4d2b86d501eb1dbf
[21:11:03] theRoUS: any road, the `gem` command is broken, and reinstalling ruby, libyaml, rubygems, and ruby-libs has no effect. just typing `gem` fails, much less the `gem pristine bundler` i need to do. :-(
[21:19:49] leftylink: it seems it is being ignored a lot. WHAT IF!!! something automatically took the content and put it on another paste service..!!!!!!!!!!!!!!
[21:20:45] leftylink: would that get complaints of "I didn't consent for you to take my content and put it elsewhere"
[21:21:02] theRoUS: phaul: foo: `cd /usr/lib64 ; ln -s libruby.so.2.5.5 libruby.so.2.4` worked around it. the fedora-supplied rubygem-psych wants libruby.so.2.4; faking it out seems to make it wonky-work
[21:28:29] phaul: leftylink: maybe. it feels very specific to copy pastebin pastes (because that's the only thing rubybot cares about) to anywehere else. Imho that thing should just not be said any more. But I know the issues with this. rubybot is our legacy, and we are waiting for apeiros to return and decide how to move forward... And when rubydoc was born I didn't just put it into rubybot because its all in ruby2.3 I think
[22:25:00] adam12: Net: If they are flow control, don't raise the exception. You could use a struct called Result that has a reason being one of those symbols. Or you can just return the symbol.. I am not sure there's any real common idiom.
[22:25:23] adam12: Net: As a third option, you could look at using a result monad. There's a bunch around: Resonad or Dry-monads being 2 gems.
[23:51:37] ivanskie: hi is there a way to make siure hash.to_json always makes everything a string, and wraps everything in quotes?
[23:55:54] leftylink: hmm, although that doesn't sound like something I would personally want (I would prefer that integers should stay integers and bools should stay bools), if I wanted to do that, I would use WELL GUESS I DON'T NEED TO TYPE AN ANSWER NOW