#ruby - 12 December 2018
« Back 1 day Forward 1 day »
[01:36:40] smhuber: if there's actually interest in something like that then I'll invest more time and port to C as a real feature request for Ruby
[01:46:03] baweaver: Considering how it conflicts with the regular pipe operator it probably won't happen. I know your code doesn't strictly touch it but that still overloads the operator which they're pretty touchy on
[01:48:39] smhuber: baweaver true although that's just an alias for syntactic sugar, the real method is __pipe__. I was curious and did take a look at the existing `|` definitions in the stdlib, looks like there's 10 and all of them expect an argument to be passed to it https://gist.github.com/shuber/b4d0a10c272d36f6b482ffd491e4cfba
[01:58:15] smhuber: ah ok gotcha - do you like the new proc composition and method reference operators that were proposed? the `<<` and `>>` and `map(&Method.:sqrt)`?
[02:09:03] smhuber: al2o3-cr gotcha yea they always look a little strange at first but then end up growing on me lol, thanks for your feedback, much appreciated!
[02:18:02] smhuber: al2o3-cr maybe it was designed to be more "expressive" then "simple"? from https://www.ruby-lang.org/en/about/ - He [Matz] has often said that he is "trying to make Ruby natural, not simple," in a way that mirrors life. Building on this, he adds: Ruby is simple in appearance, but is very complex inside, just like our human body.
[02:22:13] smhuber: more natural in english - read left to right, no other visual noise like parens and temporary vars
[02:26:26] smhuber: nice lol yea it's been around awhile :P - alrighty cool thanks again for the feedback!
[02:39:50] smhuber: al2o3-cr Oh lol I only made this just to prove it could be done - I got tired of reading comments like "Due to that any normal method call will be instantly evaluated so there is no way we will get syntax as nice as Elixir's pipe operator...". But once it was all said and done I thought the syntax felt pretty natural/elegant/concise and could possi
[02:39:50] smhuber: bly be a good fit as a core Ruby feature like it has been for many other languages
[02:40:36] smhuber: al2o3-cr my curiosity is completely satisfied - but if there is any interest in making this a real feature then I'll fork Ruby and put in the work, otherwise no big deal to me :P
[03:06:09] smhuber: al2o3-cr - agreed - I didn't really like the idea of having to stream _all the time_ like that! I like Ruby style OOP method chaining - it already reads left to right which is very natural.
[03:06:26] smhuber: al2o3-cr The pipe syntax just feels handy when you need to call out to methods on other objects in chains like that - which in a world where things like decorators, presenters, and dependency inversion principles are popular - can happen pretty regularly
[04:58:27] marz_d`ghostman: Is it possible in ruby to add an attribute to OpenStruct in rspec without retaining it like only inside a context scope? https://gist.github.com/marzdgzmn/47f733d18bb8da5feac2c5db89f200ea
[05:04:08] smhuber: marz_d`ghostman take a look at https://relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks - in your case it'd probably look something like https://gist.github.com/shuber/6564217aede7b70bf874b0baf3e50af8
[05:15:33] marz_d`ghostman: smhuber: thanks. Is there any blocks I can put an `allow` statement to be shared with multiple `it`s?
[05:25:08] smhuber: marz_d`ghostman they're only evaluated when running an "it" block that lives inside that context. there's different scopes though - ":all" vs ":each" vs ":suite" - there's more details/examples in https://relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks
[05:29:10] marz_d`ghostman: smhuber: Is there a book or a resource somewhere I can read to about these things? I didn't seem to ready any of it online
[05:33:55] smhuber: marz_d`ghostman sure there's probably a bunch of books out there, I've definitely seen some on amazon. I think their online documentation has good examples though, I'd recommend just browsing thru that to get a nice overview of all the things it's capable of
[06:08:04] baweaver: RubyConf Rundown - Part One: https://medium.com/@baweaver/rubyconf-rundown-pt-1-f64d35ddaefd
[07:12:49] ruby[bot]: -bb sonofentropy!*@*$#ruby-fix-your-connection *!*@84-10-10-170.static.chello.pl$#ruby-fix-your-connection
[11:36:33] marz_d`ghostman: How do I temporarily override a variable inside a context? It seems that the `before` statement on line 98 is overriding the statement and making the succeeding tests fail as it should have the value 'true' when getting out of that context: https://gist.github.com/marzdgzmn/fe94064b777c084aa4ef0284f0a46290
[11:58:55] jhass: marz_d`ghostman: it seems you're still confused as to when code inside a context/describe block vs when code inside a before/it/let block is run
[11:59:45] jhass: try with a blank spec file and putting puts statement to confirm the order of execution that you think there is
[12:12:17] marz_d`ghostman: jhass: I understand before statements on the outer context gets executed first before the before statements in an inner context. However, is there like a mechanism where the overriding variable will be local to the inner context like defining a local block variable e.g. ;x?
[12:13:36] jhass: you could probably do exactly that, but it would be quite confusing to most over just consistently using let
[12:20:47] marz_d`ghostman: jhass: Uhhm, so like use let to define a new variable on every context? that was the next step I was thinking to do, just wondering if I can do it with a similar local block variable mechanism. Guess I got into it too deep though hehe
[12:21:36] jhass: inner contexts and before blocks there can of course access let definitions of outer ones
[12:21:58] jhass: btw describe and context are technically just aliases, don't worry about differences there
[12:22:48] jhass: another scheme is to use @instance_vars in before blocks, but most people prefer let these days for its cleaner looks and lazy evaluation
[12:31:14] marz_d`ghostman: jhass: So what I've though is to define a base object on the outer most via `let`. Then just extend that object and assign it to another variable via another `let` on inner context to preserve the base object for another inner context to use. Am I doing it right though?
[12:33:18] marz_d`ghostman: Okay, guess I'll just do that cause if I modify the variable defined by the outermost let in an inner context it seems to retain the modified value. Which seems to be what should be though now that I think of it :)
[12:37:58] marz_d`ghostman: jhass: So it would be something like this: https://gist.github.com/marzdgzmn/a4ecd1abdf985d300147d7d4848feeed?
[12:38:36] jhass: you're still confused as to when code inside context/describe vs code inside let/before/it is run
[12:57:20] marz_d`ghostman: jhass: thanks, kudos to the test. I'm still currently going through it :)
[13:04:03] marz_d`ghostman: I'm starting to understand it better now. So ruby evaluates the code like in two phases, once for none test-related stuff hence 'ABFJK', then second for the test-related stuff starting with the `before` statement going to the `it` while lazily evaluates any `let` definitions
[13:23:12] marz_d`ghostman: it still gives the same output though, am just gonna save this code for future reference, really appreciate you clearing me from my confusion mate. thanks
[15:31:44] wlan2: So I was returning to the koans after some time off them due to work. I find myself stranded in the face of about_scoring_project.rb with a big doubt.
[15:40:03] Inside: I'm looking at someone's solution for this specific test file and there is nothing language specific in there
[15:44:34] wlan2: Well I am 100% sure that I can do it with a crapload of if. And also completely sure I don't want to write a crapload of if nor do I want to copy someone else's solution.
[15:52:28] wlan2: I'm not sure how to approach it. up until now I put a for with a couple if inside.
[15:54:38] Inside: One of ruby's greatest features is the HUGE standard library which comes with functions for almost every data manipulation you can think of
[15:58:08] phaul: nitpick, for sake of correctness... Enumerable is a module, and Array includes it, not subclasses.
[16:01:18] phaul: it's a bit nebulous, but if you can give of concrete instance examples that implies class. Usually if there are no instances it's module
[16:01:44] phaul: there is [1,2,3] an Array, so Array has to be class. There is no concrete instance of an Enumerable
[16:03:53] wlan2: Well I'll take this as an iI should make another cup of tea and use that sexy count method (or was it function?)
[16:20:03] Moeh: Hi! In my ruby app I'd like to create a class called "File" which should not 'override' the default Ruby File class. Is there a way to access the default Ruby File class within my app using something like Ruby::File (which doesn't work). Or inherit from the default Ruby File class using something like "class File < File # inherits from Ruby File class" (which also doesn't work).
[16:21:23] phaul: First I would put the entire APP in it's own namespace. Then nothing you do collide with anybody elses stuff, including gems, or stdlib
[16:23:57] phaul: you can achive this by wrapping all your classes , modules inside module MyApp ... end in all your files
[16:30:05] Moeh: I am using https://github.com/davetron5000/gli to build a CLI and it looks like it already wraps everything in a "class App" which then loads modules. But it looks like inside of the class definition it'd refer to itself. So in: "module MyModule class File File.basename() end end" File actually refers to itself (and throws a method undefined error). However, when I use "::File" inside it refers to the stdlib and works.
[16:30:34] Moeh: Yes, thanks, makes sense! I'll use "::File" inside of the class definition and then everything works.
[16:32:00] Moeh: Outside of the class definition the names are correctly scoped, so if I do "File" outside of the definition I get the stdlib File and MyModule::File returns my own class as expected. Perfect, thanks a lot!
[17:05:10] phaul: Moeh, if you are interested, there are some obscure pitfalls around this, namely the difference between module MyApp; class MyClass; end; end; vs class MyApp::MyClass; end. If you write the latter, and use File you don't need ::File, you can use File, but for the former you need the ::
[17:05:57] phaul: I explain why here https://github.com/phaul/ruby_101/blob/master/content/book.md#lexical-scope-nesting
[17:43:37] graft: hey all, i often have trouble with the case statement when I'm trying to match a class, e.g., x = Float; case x; when Float;...;end
[17:44:03] graft: the behavior here is that the when Float does not match, because x is not a Float, it is a Class
[17:46:33] phaul: graft: depends on what you want. class implements === as being kind_of? . so ruby is right. Float is not kind of Float
[17:46:54] graft: sure, i understand what it's doing, i'm just wondering if there is a way around this behavior
[17:48:39] phaul: I agree it's a bit confusing that just beacuse a == b it's not necesarrily a === b
[18:03:33] phaul: graft, there is a boolean statement form of case, it's a bit like syntactic sugared if ... if else ..
[18:07:20] baweaver: Also if anyone's looking for some RubyConf videos to start with: https://medium.com/@baweaver/rubyconf-rundown-pt-1-f64d35ddaefd
[18:38:50] slima: Hi, I try to use Nokogiri to parse xml and make changes for some values, But I completly lost, I have xml with structure: https://gist.github.com/slima/34d684fe3fd7dd016301a93a5782370d and I like to get values for first DictionaryEntry, but I can't: https://gist.github.com/slima/3828d66178c10be259b607648f64be89
[20:19:46] mr_blue: something strange happen with dotenv gem. It the .env variable was bash exported, the gem does not load the .env file value
[20:56:49] slima: al2o3-cr: I like a modify a values like <ISP>, <VID> so: search for <VID></VID> in first DictionaryEntry and insert 3 for example
[21:19:42] krawchyk: https://rubocop.readthedocs.io/en/latest/cops_performance/#performancechainarrayallocation
[21:20:07] krawchyk: does this mean `Enumerable#each_with_object` is more performant than `reduce` or `inject`?
[21:20:48] krawchyk: because each_with_object doesnt instantiate new accumulator objects each iteration
[21:21:37] havenwood: krawchyk: reduce/inject also have a persistent accumulator that's being mutated - just like each_with_object
[21:22:36] havenwood: krawchyk: there are various performance and memory implications of having frozen arrays non-mutating methods versus unfroze arrays that mutate
[21:23:35] havenwood: krawchyk: i'd suggest that you do your own benchmarking and check the actual memory and performance implications for your case
[21:25:15] havenwood: krawchyk: I did a PR a long time ago that changed Rails to use mutating methods. It was eventually rejected with a shrug.
[21:26:36] havenwood: krawchyk: an aside, but tenderlove just proposed a commit for Ruby that would allow for a frozen_hash_and_array_literal magic comment.
[21:27:30] havenwood: (I do think frozen_hash_literal should probably be separate from frozen_array_literal with a catch-all frozen_literals.)
[21:37:32] al2o3-cr: slima: changing an attribute doc.at_xpath("//key").attribute("classname").value = "i am now something else"
[23:31:29] phaul: Ookma-Kyi in the future try pasting error logs / code on dpaste.de or gist.github.com etc. Flashing animated gifs with jpeg screenshots of screen contents are not going to be popular with people trying to help.
[23:32:28] phaul: Also try to explain what you are doing, unless someone has seen the exact same backtrace it's unlikely that anybody could help
[23:35:52] ruby[bot]: How to ask the right questions to get you the right answer: https://www.mikeash.com/getting_answers.html