Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
198: Setting Up gRPC in Ruby
Which gems do I actually need to get this running?
You don't need a massive list of dependencies, but there's a distinction you need to make between the runtime and the development tools. For the actual application to run, you need the grpc gem. However, to actually generate the Ruby code from your service definitions, you need grpc-tools. I usually put grpc-tools in my development group in the Gemfile because you don't want that overhead in your production environment.
gem 'grpc'
gem 'grpc-tools', group: :development
Once those are installed, you're set. The grpc-tools gem provides the grpc_tools_ruby_protoc binary, which is the engine that reads your Protocol Buffer files and spits out the Ruby classes you'll use to build your server and client.
How do I define the service and generate the Ruby files?
gRPC is "contract-first," meaning we don't start with Ruby code; we start with a .proto file. Let's say we're building a Ride-Sharing app. We need a service that allows a passenger to request a ride. I'd create a file called ride_service.proto like this:
syntax = "proto3";
package rideshare;
service TripService {
rpc RequestRide (RideRequest) returns (RideResponse) {}
}
message RideRequest {
string passenger_id = 1;
string pickup_location = 2;
string destination = 3;
}
message RideResponse {
string trip_id = 1;
string driver_name = 2;
int32 estimated_arrival_minutes = 3;
}
Now, here is the part that trips people up: you have to compile this. You run a command in your terminal to generate the Ruby "stubs." I recommend outputting them into a lib directory so they stay organized.
grpc_tools_ruby_protoc -I . --ruby_out=lib --grpc_out=lib ride_service.proto
This creates two files in your lib folder: ride_service_pb.rb (the data messages) and ride_service_services_pb.rb (the service definition). Don't try to edit these files manually; if you change the proto file, just run the command again and overwrite them.
How do I turn those generated files into a functioning server?
Now that we have the stubs, you need to implement the actual logic. You do this by creating a class that inherits from the generated Service class. In our ride-sharing example, the generated class will be Rideshare::TripService::Service.
I've found that it's cleanest to separate the service logic from the server startup code. Here is how I'd implement the server:
require 'grpc'
require_relative 'lib/ride_service_pb'
require_relative 'lib/ride_service_services_pb'
class TripServer < Rideshare::TripService::Service
def request_ride(ride_req, _call)
puts "Processing ride request for passenger #{ride_req.passenger_id}..."
# In a real app, you'd query a database or a driver-matching algorithm here
Rideshare::RideResponse.new(
trip_id: "TRIP-#{rand(1000)}",
driver_name: "Alex",
estimated_arrival_minutes: 5
)
end
end
def main
s = GRPC::RpcServer.new
s.add_http2_port('0.0.0.0:50051', :this_port_is_secure => false)
s.handle(TripServer.new)
puts "Ride server running on 0.0.0.0:50051..."
s.run_till_terminated_or_interrupted([1])
end
main
Note the _call parameter in the request_ride method. Even if you don't use it, gRPC passes metadata and call-specific info there. I use the underscore prefix to tell Ruby (and other devs) that I'm intentionally ignoring that argument.
📋 Practical Task
Implement a Driver Status Update Service
You need to expand the ride-sharing system. Currently, we can request a ride, but the server doesn't know if drivers are available. Your task is to add a new RPC method to the existing gRPC setup.
- Modify the Proto: Update
ride_service.prototo include a new RPC method calledUpdateDriverStatus. It should take aDriverStatusRequest(containingdriver_idandis_available) and return aStatusResponse(containing asuccessboolean). - Regenerate: Run the
grpc_tools_ruby_protoccommand to update your Ruby stubs. - Implement the Logic: Update the
TripServerclass to handle theupdate_driver_statuscall. For now, simply print the driver's ID and availability to the console and return a successful response. - Test: Start your server and ensure it boots without errors now that the new method is implemented.
There are no comments for now.