The Crystal programming language | http://crystal-lang.org | Fund Crystal's development: http://is.gd/X7PRtI | Docs: http://crystal-lang.org/docs/ | API: http://crystal-lang.org/api/
favicon.ico
to the param? https://carc.in/#/r/bs23
allocate
on particular classes, and I think that would allow a custom allocator... except that this language isn't like Rust or C++ where memory is automatically deallocated when things go out of scope, so the custom memory allocator will have to be a GC
standard:
3.14user 2.03system 0:01.23elapsed 418%CPU (0avgtext+0avgdata 42320maxresident)k
1986inputs+0outputs (0major+10215minor)pagefaults 0swaps
nested_scheduler:
3.26user 2.06system 0:01.28elapsed 415%CPU (0avgtext+0avgdata 40480maxresident)k
1790inputs+0outputs (0major+9672minor)pagefaults 0swaps
Hello everyone, I am fairly experienced with Ruby and just getting started with Crystal. I love the experience! But I have a question about the approach. I am porting an existing Ruby app to Crystal, and in the original app, we use hashes to pass an arbitrary number of params.
For example, the original Ruby it looks like this:
def initialize(opts = {})
@x = opts[:has_x] ? true : false
@y = opts[:y]
end
The problem is that there are lots of options. So I am trying to figure out the most "Crystal-eque" way to do something like this.
I've considered using splats/NameTuples, but the problem is that each option has a different type, so that doesn't work.
I've considered a Hash, but the problem is that hash is complicated, and I would have to pass many options every time I call the method.
I am now considering using a structs with default values, like so:
struct UserOpts
property x
property y
def initialize(@x = true , @y = "default")
end
end
This way, I can pass the method a struct with default values, like so: initialize(opts : UserOpts)
Is there a better way to handle this?
I suspect this is a common issue from people from Ruby, so I wanted to see if there is a recommended best practice for this situation in Crystal.
def initialize(@has_x : Bool, @y : Int32)
My issue with **kwargs was that it uses a NameTuple, which require the types to the same. It's actually quite a bit of data of different types.
I considered def initialize(@has_x : Bool, @y : Int32)
approach, but that would change the method signature by a lot, add a lot of paramters, and require a lot more rewriting. But now that I think it over, this may be simplest in the long term.
:point_up: Edit: Hello everyone, I am fairly experienced with Ruby and just getting started with Crystal. I love the experience! But I have a question about the approach. I am porting an existing Ruby app to Crystal, and in the original app, we use hashes to pass an arbitrary number of params.
For example, the original Ruby it looks like this:
def initialize(opts = {})
@x = opts[:has_x] ? true : false
@y = opts[:has_y]
end
The problem is that there are lots of options. So I am trying to figure out the most "Crystal-eque" way to do something like this.
I've considered using splats/NameTuples, but the problem is that each option has a different type, so that doesn't work.
I've considered a Hash, but the problem is that hash is complicated, and I would have to pass many options every time I call the method.
I am now considering using a structs with default values, like so:
struct UserOpts
property x
property y
def initialize(@x = true , @y = "default")
end
end
This way, I can pass the method a struct with default values, like so: initialize(opts : UserOpts)
Is there a better way to handle this?
I suspect this is a common issue from people from Ruby, so I wanted to see if there is a recommended best practice for this situation in Crystal.
:point_up: Edit: Hello everyone, I am fairly experienced with Ruby and just getting started with Crystal. I love the experience! But I have a question about the approach. I am porting an existing Ruby app to Crystal, and in the original app, we use hashes to pass an arbitrary number of params.
For example, the original Ruby it looks like this:
def initialize(opts = {})
@x = opts[:has_x] ? true : false
@y = opts[:y]
end
The problem is that there are lots of options. So I am trying to figure out the most "Crystal-eque" way to do something like this.
I've considered using splats/NameTuples, but the problem is that each option has a different type, so that doesn't work.
I've considered a Hash, but the problem is that hash is complicated, and I would have to pass many options every time I call the method.
I am now considering using a structs with default values, like so:
struct UserOpts
property x
property y
def initialize(@x = true , @y = "default")
end
end
This way, I can pass the method a struct with default values, like so: initialize(opts : UserOpts)
Is there a better way to handle this?
I suspect this is a common issue from people from Ruby, so I wanted to see if there is a recommended best practice for this situation in Crystal.
:point_up: Edit: Hello everyone, I am fairly experienced with Ruby and just getting started with Crystal. I love the experience! But I have a question about the approach. I am porting an existing Ruby app to Crystal, and in the original app, we use hashes to pass an arbitrary number of params.
For example, the original Ruby it looks like this:
def initialize(opts = {})
@x = opts[:has_x] ? true : false
@y = opts[:y]
end
The problem is that there are lots of options. So I am trying to figure out the most "Crystal-eque" way to do something like this.
I've considered using splats/NameTuples, but the problem is that each option has a different type, so that doesn't work.
I've considered a Hash, but the problem is that hash is complicated, and I would have to pass many options every time I call the method.
I am now considering using a structs with default values, like so:
struct UserOpts
property x
property y
def initialize(@has_x = true , @y = "default")
end
end
This way, I can pass the method a struct with default values, like so: initialize(opts : UserOpts)
Is there a better way to handle this?
I suspect this is a common issue from people from Ruby, so I wanted to see if there is a recommended best practice for this situation in Crystal.