4 ms·
Would you post an equivalent code in Ruby ? (that like above does validation of inputs)
by qaq 7y ago
Would you post an equivalent code in Ruby ? (that like above does validation of inputs)
- johnlinvc 7y agoNot exactly the same, since there's no UInt8 in Ruby. require 'sinatra' get %r{/hello/([^/]+)/(\d+)} do |name, age| "Hello, #{age} year old named #{name}!" end
- tinco 7y agoIf you need input validation like that, which you normally wouldn't in Ruby because your models should do it for you, I'd probably use grape or something similar: require 'grape' params do requires :age, type: Integer requires :name, type: String end get "/hello/:name/:age" do "Hello, #{params[:age]} year old named #{params[:name]}!" end In any case, my beef is more with the macro than with the function annotation, which is rather spiffy. The macro hides the fact that string manipulation in Rust definitely requires a little bit of manual reading. And I feel that as soon as your app becomes more complex Rust will just start getting more in the way. And to an experienced Rust dev that might not be a big deal, because an experienced dev knows how to work with strings or which memory management strategy so it won't bog them down. But if you're just working on something and you quickly want to whip out a service that tells people what age they are, I'd definitely go for a quick Ruby or Go service.
- steveklabnik 7y agoOne of the things this API can do for you is not give you just strings, but fully typed instances of structs. And you don’t need to do the conversions yourself. So you’re not wrong, but it’s easier than you may imagine.