Generate QR Codes with Ruby
Ruby's rqrcode gem makes QR code generation simple and elegant. Perfect for Rails applications, scripts, and automation workflows.
Installation
Install the rqrcode gem using Bundler or gem install.
gem 'rqrcode'gem install rqrcodeGenerate QR Codes with rqrcode
Code examples using the rqrcode gem in Ruby.
require 'rqrcode'
qr = RQRCode::QRCode.new('https://qrcode.fun')
svg = qr.as_svg(
offset: 0,
color: '1A2B3C',
shape_rendering: 'crispEdges',
module_size: 6,
standalone: true
)
File.write('qrcode.svg', svg)require 'rqrcode'
qr = RQRCode::QRCode.new('https://qrcode.fun')
png = qr.as_png(
bit_depth: 1,
border_modules: 4,
color_mode: ChunkyPNG::COLOR_GRAYSCALE,
color: 'black',
file: nil,
fill: 'white',
module_px_size: 6,
resize_exactly_to: false,
resize_gte_to: false,
size: 300
)
IO.binwrite('qrcode.png', png.to_s)class QrCodesController < ApplicationController
def show
qr = RQRCode::QRCode.new(params[:data] || 'https://qrcode.fun')
svg = qr.as_svg(module_size: 6, standalone: true)
render inline: svg, content_type: 'image/svg+xml'
end
endGenerate QR Codes via API in Ruby
Call the QRCode.fun API from Ruby using Net::HTTP or Faraday for styled QR codes.
require 'net/http'
require 'json'
uri = URI('https://qrcode.fun/api/generate-qr-styled')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request.body = {
data: 'https://qrcode.fun',
width: 300,
height: 300,
type: 'png',
margin: 10,
dotsOptions: { color: '#1A2B3C', type: 'rounded' },
cornersSquareOptions: { color: '#8564C3', type: 'extra-rounded' },
backgroundOptions: { color: '#FFFFFF' }
}.to_json
response = http.request(request)
result = JSON.parse(response.body)
puts result['data'][0..49]Live QR Code Preview
Try generating a QR code with Ruby right now.
Native Library vs API
Compare using rqrcode versus the QRCode.fun API.
| Feature | rqrcode Gem | QRCode.fun API |
|---|---|---|
| Setup complexity | gem install + ChunkyPNG for images | Single HTTP request |
| Customization | Colors, module size (SVG/PNG) | Full styling: colors, shapes, logos |
| Offline support | Yes | Requires internet |
| Maintenance | bundle update | Always up-to-date |
| Output formats | SVG, PNG, ANSI terminal | PNG, SVG |
Ruby QR Code Use Cases
Common scenarios for QR codes in Ruby applications.
Rails Applications
Generate QR codes in Rails controllers for user profiles, event tickets, and two-factor authentication setup.
Background Jobs
Create QR codes asynchronously with Sidekiq or Active Job for batch label generation and email campaigns.
API Endpoints
Build API endpoints that return QR codes as images or SVG for mobile apps and third-party integrations.
Automation Scripts
Write Ruby scripts for generating QR codes in bulk for inventory, asset tagging, and event management.
Frequently Asked Questions
Common questions about generating QR codes with Ruby.
Start generating QR codes with Ruby
Use our free generator or integrate the API into your Ruby and Rails applications.