<FromGitter>
<Timbus> @jspillers .. Neat. I would consider defining it as `.to_h(stringify: true)`, or something. Your broader 'conversion' suggestion ( `.to_h(String,String)` ) would require a whole converter suite.. similar to dlang's excellent std.conv toolkit.
pabs has quit [Ping timeout: 240 seconds]
pabs has joined #crystal-lang
<FromGitter>
<fridgerator> @drum445 whats wrong with mappings?
<FromGitter>
<jspillers> @Timbus oh right... great suggestion - i forgot method signature overloading is a thing here (solid name btw - i approve :) )
hplar has joined #crystal-lang
<FromGitter>
<Timbus> Just as a side note, you can also simplify a little bit: `string_keys = keys.map {|k| k.to_s }.to_a` -> `string_keys = keys.flat_map &.to_s`.. and yes, rare to see another Jarrod :)
<FromGitter>
<bew> Huh why did you change `map` to `flat_map`?
<FromGitter>
<bew> Ah it directly builds an array
<FromGitter>
<bew> That's weird, and looks like a bug
<FromGitter>
<Timbus> oh, yeah
<FromGitter>
<bew> I mean, it's not meant to be used to solve this problem, and just reading the code using it, it's not clear what it's doing and why..
<FromGitter>
<Timbus> I didn't even think about it, I just figured "flatmap always returns arrays"
<FromGitter>
<aisrael> TIL `&.method` shortcut
<FromGitter>
<Timbus> its super nice ey
<FromGitter>
<aisrael> I mean I knew about `&:method` in Ruby, just didn't realize the Crystal equivalent 👍
<FromGitter>
<Timbus> it's not exactly the same, eg: `[1,2,3].map &.* 2`
<FromGitter>
<Timbus> unless ruby can do that?
duane has joined #crystal-lang
<FromGitter>
<vlazar> yes, you can make Ruby do the same but Crystal's version is better
<FromGitter>
<drum445> @fridgerator nothing wrong with mapping just having to add DB mapping and JSON mapping to each class makes them look very messy
rohitpaulk has quit [Ping timeout: 244 seconds]
rohitpaulk has joined #crystal-lang
<FromGitter>
<drum445> How do you guys go about getting data from a db? Do you use the ? syntax and rs.read(T)
rohitpaulk has quit [Ping timeout: 240 seconds]
<crystal-gh>
[crystal] straight-shoota opened pull request #6369: Add location as required argument for Time.parse (master...jm/feature/time-parse-location) https://git.io/fNLJm
<FromGitter>
<aisrael> @drum445 What do you mean "? syntax"? I just go `db.query_all("SELECT foo FROM bar") do |rs|` and then, yes, `rs.read(T)`
<FromGitter>
<drum445> So "select * from foo where bar = ? and x = ? and y = ?", "bar", 32, 45 do |rs|
<FromGitter>
<drum445> I guess my trouble is that when selecting a lot of data rs.read becomes a bit unreadable
<FromGitter>
<aisrael> Yeah. Well, that's why I'm writing something like Ruby's Sequel or Java's jOOQ. While I appreciate e.g. Granite ORM 1) I think there's room for an alternative approach (not AR-style ORM, just SQL generation and mapping), 2) I'm wary about how far the macro approach can go. ActiveRecord is able to work its magic because Ruby. jOOQ and Sequel use purely OO approach (no meta programming).
<crystal-gh>
[crystal] straight-shoota opened pull request #6371: Add ECR.render for rendering without setting up an IO (master...jm/feature/ecr-render) https://git.io/fNLUc
<FromGitter>
<aisrael> So far I have query generation (PostgreSQL only), working on mapping next. Will probably have something like Sequel's `Dataset`
<FromGitter>
<drum445> Just hit a wall though where every class attr needs to be union type nil for it to work which isn't ideal
<FromGitter>
<drum445> But no idea of a way around it
<FromGitter>
<aisrael> Use the constructor route? I mean, that's how I
<FromGitter>
<drum445> What do you mean sorry?
<FromGitter>
<aisrael> Um... admittedly Crystal doesn't (yet) have reflection but maybe you can do it at compile time with macros. Like, `Objectify.to_object(rs, klazz)` ends up calling `klazz.new(*args)`
<FromGitter>
<drum445> Yeah I was banking on reflection, something like .send in ruby. ⏎ How would the initializer look on klazz if I'm passing args?
<FromGitter>
<aisrael> No different from normal? I mean, you (obviously) can't introspect the class to find out it's attributes and look for an appropriate constructor—but using macros maybe you can, or you can just go ahead and generate one
<FromGitter>
<aisrael> @LVMBDV Sure. Yeah, I guess I meant to say it doesn't have as rich (runtime) reflection (though, I'll settle for compile-time and macros if it serves my needs) as Ruby or Java—enumerate a class' methods, constructors, etc.
<lvmbdv>
runtime reflection would add a lot of overhead tho
<lvmbdv>
but i see your point
<FromGitter>
<aisrael> I understand. Which is why I thought maybe Crystal can its magic at compile-time. That, or maybe make it a flag of some sort. Maybe even a mixin/module, e.g. `include RuntimeReflection` ;)
<FromGitter>
<drum445> @LVMBDV yeah we meant runtime reflection
<lvmbdv>
You could make a shard for that :)
<FromGitter>
<aisrael> How to redefine `def` though. A macro (`def_with_reflection`), somehow?
<lvmbdv>
to keep a record of macros?
<lvmbdv>
could add a `method_added` hook to Class/Object/Value (iunno which) instead
<FromGitter>
<aisrael> 💡
<FromGitter>
<aisrael> So... if I wanted to avoid having to type `Module::Foo` everywhere, is the recommended way still `alias Foo = Module::Foo`?
<FromGitter>
<Grabli66> Hi! I tried to cross-compile my program to arm. I got an object file and string which i need launch on target OS. And i saw a "/usr/share/crystal/src/ext/libcrystal.a" in that string. But there is no librystal.a on target OS. Where i can find it? :)
<FromGitter>
<SamualLB> Hello, I am trying to create an ncurses macro to generate the F keys. I am currently trying to declare this in a Lib enum.
<FromGitter>
<SamualLB> I have tried this inside and outside the enum but I get errors for reopening an enum and can't declare type inside enum, does anybody know an solution? Thanks
rohitpaulk has quit [Ping timeout: 248 seconds]
<crystal-gh>
[crystal] myfreeweb opened pull request #6373: Add support for FreeBSD/aarch64 (master...freebsd-aarch64) https://git.io/fNL8q
<crystal-gh>
[crystal] straight-shoota opened pull request #6374: Add pp! and p! macro methods (master...jm/feature/macro-pp) https://git.io/fNL0u
alex``` has joined #crystal-lang
wontruefree has joined #crystal-lang
wontruefree has quit [Client Quit]
lvmbdv has quit [Ping timeout: 240 seconds]
<FromGitter>
<fridgerator> @drum445 You can do both mappings at the same time with a macro
lvmbdv has joined #crystal-lang
wontruefree has joined #crystal-lang
wontruefree has quit [Client Quit]
duane has joined #crystal-lang
<FromGitter>
<martinos> I am wonder if there's a way to write generic procs. I would like to create some Proc as such: `id = ->(a) {a}`. Is this possible ?
<FromGitter>
<straight-shoota> @Grabli66 Run `make libcrystal` in Crystal directory on target platform.
wontruefree has joined #crystal-lang
<FromGitter>
<straight-shoota> @martinos Procs need to be fully typed because of their nature, the types can't be automatically inferred.
wontruefree has quit [Client Quit]
wontruefree has joined #crystal-lang
<FromGitter>
<straight-shoota> Depending on what you want to do, there might be workarounds, though
<FromGitter>
<martinos> Compiled functional languages can do it. It's possible to do but maybe not with Crystal.
<FromGitter>
<martinos> I wished that I could translate a lib that I have done in Ruby. Thanks
<FromGitter>
<straight-shoota> Yes, it's not systematically impossible, but Crystal can't do it (currently).
<FromGitter>
<straight-shoota> This is somewhat similar to ivar types. Theoretically, you should be able to have a ivar type `Object`, but it's not possible yet.
wontruefree has quit [Quit: bye]
alex``` has quit [Quit: WeeChat 2.1]
wontruefree has joined #crystal-lang
lvmbdv has quit [Ping timeout: 260 seconds]
<crystal-gh>
[crystal] ysbaddaden opened pull request #6376: Proof of Concept: detect stack overflows (x86_64-linux-gnu target only) (master...poc-detect-stack-overflow) https://git.io/fNLDg
<wontruefree>
I dont know much about segfaults except it happens when you try to access protected memory. If you are in the segfault handler is it ok to allocate a new object?
<FromGitter>
<fridgerator> The Benevolent Dictator For Life - No More
wontruef_ has joined #crystal-lang
<wontruef_>
yeah no joke
wontruef_ has quit [Client Quit]
wontruef_ has joined #crystal-lang
wontruefree has quit [Ping timeout: 256 seconds]
<FromGitter>
<asterite> I understand him. Discussions in issue trackers make you really tired. You want to create a language, or change something in it because you learned from past mistakes, but there will be many that will argue those decisions, and then you get angry, furious, etc. I actually did the same him as he, I just didn't announce it. I give my comments, I send PR, but I don't merge code anymore not approve PRs.
<FromGitter>
<asterite> I let the community direct the language, which seem to know better how to do things
<Yxhuvud>
well, lets hope it ends up working out. sometimes controversial changes must be made and those are hard to get past a design that is by commitee
<FromGitter>
<asterite> like `case` without `when` and `else` :-P . I'm not sure about that change, but I don't care much anymore, it's just a tool
<Yxhuvud>
well, that one is pretty uncontroversial in that it doesn't break anything and noone is expected to write code like that manually.
<FromGitter>
<asterite> true
duane has quit [Ping timeout: 244 seconds]
wontruef_ has quit [Quit: bye]
wontruefree has joined #crystal-lang
lvmbdv has quit [Ping timeout: 268 seconds]
duane has joined #crystal-lang
<FromGitter>
<solisoft> Hi there
<FromGitter>
<solisoft> I would like to convert an ISO-8859-15 string to UTF8
<FromGitter>
<solisoft> I tried mystring.encode "UTF8" but it raise an error
<FromGitter>
<solisoft> any help will be welcome
<FromGitter>
<solisoft> the error is : `#<ArgumentError:Invalid multibyte sequence>`
<FromGitter>
<straight-shoota> It looks like your string contains an invalid multibyte sequence and can't be converted to ISO-8859-15. The API docs explain this: You need to specify `:skip` as parameter to skip invalid sequences. https://crystal-lang.org/api/0.25.1/String.html#encode%28encoding%3AString%2Cinvalid%3ASymbol%3F%3Dnil%29%3ABytes-instance-method
<FromGitter>
<solisoft> well running ` iconv -f ISO-8859-15 -t UTF-8 demo.txt` in my terminal works fine
<FromGitter>
<solisoft> I don't want to loose some chars
<FromGitter>
<solisoft> it's a french text with some accents
<FromGitter>
<solisoft> there is a way to do that ?
<FromGitter>
<solisoft> if not, I'll create a big file and I'll will convert it using iconv
<FromGitter>
<straight-shoota> can you show a minimal reproducible sample?
<FromGitter>
<straight-shoota> `String#encode` uses `libiconv`. I'm pretty sure that the `iconv` CLI tool can only do things the lib can do.
<FromGitter>
<bew> Looks like his string is initially in iso-xxxxxx format, and you want it in utf8 right?
<FromGitter>
<bew> So it should be decode instead of encode?
<FromGitter>
<solisoft> Ah maybe
<FromGitter>
<bew> Hmm doesn't exist..
<FromGitter>
<bew> Where is your string coming from?
<FromGitter>
<solisoft> raise ; Unhandled exception: Nil assertion failed (Exception) ⏎ from /usr/local/Cellar/crystal-lang/0.25.0_1/src/nil.cr:106:5 in 'not_nil!' ⏎ from /usr/local/Cellar/crystal-lang/0.25.0_1/src/http/client/response.cr:9:3 in 'body_io' ⏎ from src/fetch_data.cr:14:5 in '__crystal_main' ⏎ from /usr/local/Cellar/crystal-lang/0.25.0_1/src/crystal/main.cr:104:5 in 'main_user_code' ...
<FromGitter>
<solisoft> ok nevermind ... saving raw output in a file
<FromGitter>
<solisoft> i'll convert it then i'll load it in an another script
<FromGitter>
<solisoft> thanks
<FromGitter>
<straight-shoota> That's strange, why is `body_io` not set...
<FromGitter>
<straight-shoota> Anyway, if you have a string in non-utf-8, you can just decode it using `String.new(iso_string.to_slice, "ISO-8859-1")`
<FromGitter>
<solisoft> Great worked just fine
<FromGitter>
<solisoft> Thanks
<FromGitter>
<asterite> body_io only works in the block form
<FromGitter>
<asterite> so something like: `HTTP::Client.get("...") { |response| response.body_io.set_encoding("..."); if response.status_code == 200; etc. }` should work
<FromGitter>
<asterite> I find it funny the the encoding comes in an html tag, but to understand that you need to know the encoding...
<FromGitter>
<asterite> we can probably improve `body_io` to raise a description like "body_is is only available in the block form" if it's nil...
ashirase has quit [Ping timeout: 244 seconds]
<FromGitter>
<straight-shoota> yeah, I suspected something like that
<FromGitter>
<straight-shoota> Some API documentation would already help ;)