Skip to content

Commit e681224

Browse files
committed
Various tidying up
1 parent bd694b8 commit e681224

35 files changed

+293
-180
lines changed

Gemfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
source 'https://rubygems.org'
2-
ruby '2.5.7', :engine => 'jruby', :engine_version => '9.2.9.0'
4+
ruby '2.5.7', engine: 'jruby', engine_version: '9.2.9.0'
35
gemspec

bin/picrate

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env jruby
2+
# frozen_string_literal: true
3+
24
unless defined? PICRATE_ROOT
3-
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))
5+
$LOAD_PATH << __dir__
46
PICRATE_ROOT = File.expand_path(File.dirname(__FILE__) + '/../')
57
end
68

lib/picrate.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
PICRATE_ROOT = File.dirname(__dir__)
77
end
88

9-
Dir["#{PICRATE_ROOT}/lib/*.jar"].each do |jar|
9+
Dir["#{PICRATE_ROOT}/lib/*.jar"].sort.each do |jar|
1010
require jar
1111
end
1212
require_relative 'picrate/app'

lib/picrate/app.rb

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: false
2+
23
require_relative 'helper_methods'
34
require_relative 'library_loader'
45
# A wrapper module for the processing App
@@ -49,7 +50,10 @@ class << self
4950

5051
# All sketches extend this class
5152
class App < PApplet
52-
include Math, MathTool, HelperMethods, Render
53+
include Render
54+
include HelperMethods
55+
include MathTool
56+
include Math
5357
# Alias some methods for familiarity for Shoes coders.
5458
alias oval ellipse
5559
alias stroke_width stroke_weight
@@ -101,6 +105,7 @@ def load_java_library(*args)
101105
# Processing call them by their expected Java names.
102106
def method_added(method_name) #:nodoc:
103107
return unless METHODS_TO_ALIAS.key?(method_name)
108+
104109
alias_method METHODS_TO_ALIAS[method_name], method_name
105110
end
106111
end
@@ -113,6 +118,7 @@ def initialize(options = {}, arguments = [])
113118
# Guard against invalid input.
114119
proxy_java_fields
115120
raise TypeError unless options.is_a?(Hash) && arguments.is_a?(Array)
121+
116122
# Set up the sketch.
117123
super()
118124
post_initialize(options)
@@ -132,8 +138,7 @@ def size(*args)
132138
super(*args)
133139
end
134140

135-
def post_initialize(_args)
136-
end
141+
def post_initialize(_args); end
137142

138143
def data_path(dat)
139144
dat_root = File.join(SKETCH_ROOT, 'data')
@@ -162,6 +167,7 @@ def proxy_java_fields
162167
# Processing call them by their expected Java names.
163168
def method_added(method_name) #:nodoc:
164169
return unless METHODS_TO_ALIAS.key?(method_name)
170+
165171
alias_method METHODS_TO_ALIAS[method_name], method_name
166172
end
167173
end
@@ -179,6 +185,7 @@ def respond_to_missing?(symbol, include_priv = false)
179185

180186
def method_missing(name, *args, &block)
181187
return Processing.app.send(name, *args) if Processing.app.respond_to? name
188+
182189
super
183190
end
184191
end # Processing::Proxy

lib/picrate/creators/parameters.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ module Parameters
1111
PARAM = { 'sketch' =>
1212
{ 'width' => 640, 'height' => 480, 'mode' => 'P2D' } }.freeze
1313

14-
def self.write
15-
FileUtils.mkdir_p PATH
16-
File.write(FILE, PARAM.to_yaml)
17-
end
14+
def self.write
15+
FileUtils.mkdir_p PATH
16+
File.write(FILE, PARAM.to_yaml)
17+
end
1818

19-
def self.read
20-
write unless File.exist?(FILE)
21-
YAML.load_file(FILE)
22-
end
19+
def self.read
20+
write unless File.exist?(FILE)
21+
YAML.load_file(FILE)
2322
end
23+
end

lib/picrate/creators/sketch_factory.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
require_relative 'sketch_writer'
1+
# frozen_string_literal: true
22

3+
require_relative 'sketch_writer'
4+
# Sketch Factory
35
class SketchFactory
4-
NAMES = %w[One Two Three]
5-
def initialize(argc)
6+
NAMES = %w[One Two Three].freeze
7+
def initialize(_argc)
68
NAMES.each do |name|
79
SketchWriter.new(File.basename(name, '.rb')).write
810
end

lib/picrate/helper_methods.rb

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: false
2+
23
# processing module wrapper
34
require_relative 'helpers/numeric'
45
module Processing
@@ -52,6 +53,7 @@ def hsb_color(hue, sat, brightness)
5253

5354
def color(*args)
5455
return super(*args) unless args.length == 1
56+
5557
super(hex_color(args[0]))
5658
end
5759

@@ -66,11 +68,8 @@ def int_to_ruby_colors(hex)
6668
# Overrides Processing convenience function thread, which takes a String
6769
# arg (for a function) to more rubylike version, takes a block...
6870
def thread(&block)
69-
if block_given?
70-
Thread.new(&block)
71-
else
72-
raise ArgumentError, 'thread must be called with a block', caller
73-
end
71+
warn 'you must provide a block' unless block_given?
72+
Java::JavaLang::Thread.new(&block).start
7473
end
7574

7675
# explicitly provide 'processing.org' min instance method
@@ -94,9 +93,9 @@ def max(*args)
9493
def dist(*args)
9594
case args.length
9695
when 4
97-
return dist2d(*args)
96+
dist2d(*args)
9897
when 6
99-
return dist3d(*args)
98+
dist3d(*args)
10099
else
101100
raise ArgumentError, 'takes 4 or 6 parameters'
102101
end
@@ -117,7 +116,7 @@ def find_method(method_name)
117116
# Proxy over a list of Java declared fields that have the same name as
118117
# some methods. Add to this list as needed.
119118
def proxy_java_fields
120-
fields = %w(key frameRate mousePressed keyPressed)
119+
fields = %w[key frameRate mousePressed keyPressed]
121120
methods = fields.map { |field| java_class.declared_field(field) }
122121
@declared_fields = Hash[fields.zip(methods)]
123122
end
@@ -163,6 +162,7 @@ def save_strings(filename, strings)
163162
# frame_rate needs to support reading and writing
164163
def frame_rate(fps = nil)
165164
return @declared_fields['frameRate'].value(java_self) unless fps
165+
166166
super(fps)
167167
end
168168

@@ -178,19 +178,17 @@ def key_pressed?
178178

179179
private
180180

181-
INTEGER_COL = -> (x) { x.is_a?(Integer) }
182-
STRING_COL = -> (x) { x.is_a?(String) }
183-
FLOAT_COL = -> (x) { x.is_a?(Float) }
184181
# parse single argument color int/double/String
185-
def hex_color(a)
186-
case a
187-
when INTEGER_COL
188-
Java::Monkstone::ColorUtil.colorLong(a)
189-
when STRING_COL
190-
return Java::Monkstone::ColorUtil.colorString(a) if a =~ /#\h+/
191-
raise StandardError, 'Dodgy Hexstring'
192-
when FLOAT_COL
193-
Java::Monkstone::ColorUtil.colorDouble(a)
182+
def hex_color(arg)
183+
case arg
184+
when Integer
185+
Java::Monkstone::ColorUtil.colorLong(arg)
186+
when String
187+
raise StandardError, 'Dodgy Hexstring' unless arg.match(/#\h{6}$/)
188+
189+
Java::Monkstone::ColorUtil.colorString(arg)
190+
when Float
191+
Java::Monkstone::ColorUtil.colorDouble(arg)
194192
else
195193
raise StandardError, 'Dodgy Color Conversion'
196194
end
@@ -200,6 +198,7 @@ def dist2d(*args)
200198
dx = args[0] - args[2]
201199
dy = args[1] - args[3]
202200
return 0 if dx.abs < EPSILON && dy.abs < EPSILON
201+
203202
Math.hypot(dx, dy)
204203
end
205204

@@ -208,6 +207,7 @@ def dist3d(*args)
208207
dy = args[1] - args[4]
209208
dz = args[2] - args[5]
210209
return 0 if dx.abs < EPSILON && dy.abs < EPSILON && dz.abs < EPSILON
210+
211211
Math.sqrt(dx * dx + dy * dy + dz * dz)
212212
end
213213
end

lib/picrate/helpers/numeric.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class Numeric #:nodoc:
24
def degrees
35
self * 57.29577951308232

lib/picrate/library.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require_relative 'native_folder'
24
require_relative 'native_loader'
35
require 'pathname'
@@ -18,6 +20,7 @@ def locate
1820
return if (@path = Pathname.new(
1921
File.join(PICRATE_ROOT, 'library', name, "#{name}.rb")
2022
)).exist?
23+
2124
locate_java
2225
end
2326

@@ -46,10 +49,11 @@ def exist?
4649
end
4750

4851
def load_jars
49-
Dir.glob("#{dir}/*.jar").each do |jar|
52+
Dir.glob("#{dir}/*.jar").sort.each do |jar|
5053
require jar
5154
end
5255
return true unless native_binaries?
56+
5357
add_binaries_to_classpath
5458
end
5559

lib/picrate/library_loader.rb

+2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ def load_libraries(*args)
3333

3434
def loader(name)
3535
return true if @loaded_libraries.include?(name)
36+
3637
fname = name.to_s
3738
library = Library.new(fname)
3839
library.locate
3940
return require_library(library, name) if library.ruby?
41+
4042
warn("Not found library: #{fname}") unless library.exist?
4143
load_jars(library, name)
4244
end

lib/picrate/native_folder.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
# frozen_string_literal: true
2+
13
require 'rbconfig'
24

35
# Utility to load native binaries on Java CLASSPATH
4-
#HACK until jruby returns a more specific 'host_os' than 'linux'
6+
# HACK until jruby returns a more specific 'host_os' than 'linux'
57
class NativeFolder
68
attr_reader :os, :bit
79

8-
LINUX_FORMAT = 'linux%s'.freeze
9-
ARM32 = '-armv6hf'.freeze
10+
LINUX_FORMAT = 'linux%s'
11+
ARM32 = '-armv6hf'
1012
# ARM64 = '-aarch64'.freeze
1113

1214
def initialize
@@ -19,7 +21,7 @@ def name
1921
return format(LINUX_FORMAT, '64') if /amd64/.match?(bit)
2022
return format(LINUX_FORMAT, ARM32) if /arm/.match?(bit)
2123
end
22-
raise RuntimeError, "Unsupported Architecture"
24+
raise 'Unsupported Architecture'
2325
end
2426

2527
def extension

lib/picrate/native_loader.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# This class knows how to dynamically set the 'java' native library path
24
# It might not work with java 9?
35
class NativeLoader
@@ -21,6 +23,7 @@ def add_native_path(pth)
2123
field = JC::Class.for_name('java.lang.ClassLoader')
2224
.get_declared_field('sys_paths')
2325
return unless field
26+
2427
field.accessible = true # some jruby magic
2528
field.set(JC::Class.for_name('java.lang.System').get_class_loader, nil)
2629
end

lib/picrate/runner.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: false
2+
23
require 'optparse'
34
require_relative 'version'
45

library/boids/boids.rb

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
# Boids -- after Tom de Smedt.
34
# See his Python version: http://nodebox.net/code/index.php/Boids
45
# This is an example of how a pure-Ruby library can work. Original for
@@ -8,7 +9,8 @@ class Boid
89
attr_reader :boids
910
attr_accessor :vel, :pos, :is_perching, :perch_time
1011
def initialize(boids, pos)
11-
@boids, @flock = boids, boids
12+
@boids = boids
13+
@flock = boids
1214
@pos = pos
1315
@vel = Vec2D.new
1416
@is_perching = false
@@ -54,6 +56,7 @@ def limit(max:)
5456
# Tweet, Tweet! The boid police will bust you for breaking the speed limit.
5557
most = [vel.x.abs, vel.y.abs].max
5658
return if most < max
59+
5760
scale = max / most.to_f
5861
@vel *= scale
5962
end
@@ -89,10 +92,14 @@ def self.flock(n:, x:, y:, w:, h:)
8992

9093
def setup(n, x, y, w, h)
9194
n.times do
92-
dx, dy = rand(w), rand(h)
95+
dx = rand(w)
96+
dy = rand(h)
9397
self << Boid.new(self, Vec2D.new(x + dx, y + dy))
9498
end
95-
@x, @y, @w, @h = x, y, w, h
99+
@x = x
100+
@y = y
101+
@w = w
102+
@h = h
96103
@scattered = false
97104
@scatter = 0.005
98105
@scatter_time = 50.0
@@ -145,13 +152,15 @@ def no_goal
145152

146153
def constrain
147154
# Put them boids in a cage.
148-
dx, dy = @w * 0.1, @h * 0.1
155+
dx = @w * 0.1
156+
dy = @h * 0.1
149157
each do |b|
150158
b.vel.x += rand(dx) if b.pos.x < @x - dx
151159
b.vel.y += rand(dy) if b.pos.y < @y - dy
152160
b.vel.x -= rand(dx) if b.pos.x > @x + @w + dx
153161
b.vel.y -= rand(dy) if b.pos.y > @y + @h + dy
154162
next unless b.pos.y > perch_y && rand < perch
163+
155164
b.pos.y = perch_y
156165
b.vel.y = b.vel.y.abs * -0.2
157166
b.is_perching = true

0 commit comments

Comments
 (0)