Skip to content

Instantly share code, notes, and snippets.

Avatar

Ryo Nakamura r7kamura

View GitHub Profile
@r7kamura
r7kamura / get_json_then_convert_it_to_xml.rb
Last active Nov 24, 2020
Ruby script to GET JSON then convert it to XML.
View get_json_then_convert_it_to_xml.rb
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activesupport'
gem 'builder'
gem 'faraday'
gem 'faraday_middleware'
end
@r7kamura
r7kamura / convert_json_to_xml.rb
Last active Nov 23, 2020
Ruby script to convert JSON to XML.
View convert_json_to_xml.rb
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activesupport'
gem 'builder'
end
require 'active_support/core_ext/array/conversions'
@r7kamura
r7kamura / factory_bot_factories_spec.rb
Last active Nov 13, 2020
Example to lint FactoryBot factories with RSpec.
View factory_bot_factories_spec.rb
require 'rails_helper'
RSpec.describe 'FactoryBot factory' do
FactoryBot.factories.each do |factory|
describe factory.name.inspect do
it 'creates valid record' do
expect { FactoryBot::Linter.new([factory]).lint! }.not_to raise_error
end
end
end
View active_record_bidirectional_accepts_nested_for_test.rb
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activerecord', '6.0.3.4'
gem 'sqlite3'
end
@r7kamura
r7kamura / i18n_symbol_reference_feature_example.rb
Last active Nov 5, 2020
An example of how to reuse other values by symbol translation key value on i18n gem.
View i18n_symbol_reference_feature_example.rb
require 'i18n'
require 'yaml'
I18n.backend.store_translations(
:en,
YAML.load(<<-YAML)
activerecord:
attributes:
user:
status:
@r7kamura
r7kamura / rename_factories_to_plurals.rb
Last active Oct 28, 2020
Rename FactoryBot files from singular to plural.
View rename_factories_to_plurals.rb
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activesupport'
end
require 'active_support/inflector'
require 'pathname'
@r7kamura
r7kamura / space_probe.rb
Last active Sep 25, 2020
A Ruby script to print cop names which include "space".
View space_probe.rb
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activesupport'
gem 'rubocop', '0.84.0'
gem 'rubocop-performance', '1.6.0'
gem 'rubocop-rails', '2.5.2'
View benchmark_require_time.rb
module Kernel
alias_method :original_require, :require
def require(path)
time = ::Time.now
original_require(path).tap do
puts "%3.6f\t#{path}" % (Time.now - time)
end
end
end
View raise_kwargs_test.rb
class MyError < StandardError
def initialize(**options)
p options
end
end
raise MyError, foo: :bar