Ruby anatomy: Difference between revisions
From wikinotes
(15 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Example = | = Example = | ||
<blockquote> | <blockquote> | ||
== Sample Project == | |||
<blockquote> | |||
{{ NOTE | | |||
None of [[ruby bundler]], [[ruby gems]], [[ruby chruby]] set the <code>$LOAD_PATH</code>.<br> | |||
To add it automatically, you'll need to use something like [[direnv]] or [[nix]]. | |||
}} | |||
{{ NOTE | | |||
You may also be interested in the examples section in [[ruby]].<br> | |||
For example, [[ruby cli]]. | |||
}} | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
# project structure | |||
myproject/ | myproject/ | ||
lib/ | lib/ | ||
bar/ | bar/baz.rb | ||
foo.rb | foo.rb | ||
test/ | |||
foo_test.rb | |||
Gemfile | Gemfile | ||
.envrc | |||
</syntaxhighlight> | </syntaxhighlight> | ||
{{ | {{ expand | ||
| <code>lib/bar/baz.rb</code> | | <code>lib/bar/baz.rb</code> | ||
| | | | ||
Line 32: | Line 44: | ||
}} | }} | ||
{{ | {{expand | ||
| <code>lib/foo.rb</code> | | <code>lib/foo.rb</code> | ||
| | | | ||
Line 48: | Line 59: | ||
}} | }} | ||
{{expand | |||
| <code>test/foo_test.rb</code> | |||
| | |||
<syntaxhighlight lang="ruby"> | |||
require 'foo' | |||
require 'minitest/autorun' | |||
class FooTest < Minitest::Test | |||
def test_constant_resolves | |||
assert(defined?(Foo)) | |||
end | |||
end | |||
</syntaxhighlight> | |||
}} | |||
{{ expand | |||
| <code>Gemfile</code> | |||
| | |||
<syntaxhighlight lang="ruby"> | |||
source 'https://rubygems.org' | |||
gem 'minitest' | |||
gem 'pry' | |||
</syntaxhighlight> | |||
}} | |||
{{ expand | |||
| <code>.envrc</code> | |||
| | |||
<syntaxhighlight lang="bash"> | |||
export RUBYLIB="$(pwd)/lib:$RUBYLIB" | |||
</syntaxhighlight> | |||
}} | |||
Setup | |||
<syntaxhighlight lang="bash"> | |||
# allow env to be set by direnv | |||
direnv allow | |||
# use user-local gem cache | |||
bundle config set --local path '~/.bundle' | |||
</syntaxhighlight> | |||
Running 'foo.rb' | |||
<source lang="bash"> | <source lang="bash"> | ||
# '-I lib' sets the $LOAD_PATH so that the module can be found | |||
ruby -I lib lib/foo.rb | ruby -I lib lib/foo.rb | ||
</source> | </source> | ||
Running 'foo_test.rb' | |||
<syntaxhighlight lang="bash"> | |||
ruby -I.:test test/foo_test.rb | |||
</syntaxhighlight> | |||
</blockquote><!-- Multi File Project --> | |||
== $RUBYLIB and $LOAD_PATH == | |||
<blockquote> | |||
Instead of <code>ruby -I lib ...</code> you can use another tool to setup the <code>$LOAD_PATH</code>. | |||
[[direnv]] can set the environment <code>$RUBYLIB</code> | |||
<syntaxhighlight lang="bash"> | |||
# myproject/.envrc | |||
export RUBYLIB="$(pwd)/lib:$RUBYLIB" | |||
</syntaxhighlight> | |||
[[nix]] can manage the environment as well | |||
</blockquote><!-- $LOAD_PATH --> | |||
</blockquote><!-- example --> | </blockquote><!-- example --> | ||
Latest revision as of 19:52, 4 November 2023
Example
Sample Project
NOTE:
None of ruby bundler, ruby gems, ruby chruby set the
$LOAD_PATH
.
To add it automatically, you'll need to use something like direnv or nix.# project structure myproject/ lib/ bar/baz.rb foo.rb test/ foo_test.rb Gemfile .envrc
lib/bar/baz.rb
module Bar class Baz def initialize(name:) @name = name end def say_hi puts("hi, #{@name}") end end end
lib/foo.rb
require 'bar/baz' if __FILE__ == $0 baz = Bar::Baz.new(name: "alex") baz.say_hi() end
test/foo_test.rb
require 'foo' require 'minitest/autorun' class FooTest < Minitest::Test def test_constant_resolves assert(defined?(Foo)) end end
Gemfile
source 'https://rubygems.org' gem 'minitest' gem 'pry'
.envrc
export RUBYLIB="$(pwd)/lib:$RUBYLIB"
Setup
# allow env to be set by direnv direnv allow # use user-local gem cache bundle config set --local path '~/.bundle'Running 'foo.rb'
# '-I lib' sets the $LOAD_PATH so that the module can be found ruby -I lib lib/foo.rbRunning 'foo_test.rb'
ruby -I.:test test/foo_test.rb$RUBYLIB and $LOAD_PATH
Instead of
ruby -I lib ...
you can use another tool to setup the$LOAD_PATH
.direnv can set the environment
$RUBYLIB
# myproject/.envrc export RUBYLIB="$(pwd)/lib:$RUBYLIB"nix can manage the environment as well
basics
RUBY_VERSION() # ruby version RUBY_PATCHLEVEL() # ruby patch revision RUBY_DESCRIPTION() # ruby version 3.times {puts "hi"} # loop 3 times
reflection
myclass = MyClass.new() myclass.methods # print all methods myclass.methods - Object.methods # print non-object methods myclass.class # type myclass.__FILE__ # file class belongs to instance.method(:method_name).source_location # file method is defined in (nil means compiled) MyModule.constants.select {|c| MyModule.const_get(c).is_a? Class} # list all classes within a namespace # files module is defined in $LOADED_FEATURES.select { |file| File.read(file).include?('module Foo') rescue false }
builtins
Keywords/functions/types built in to ruby are defined within the
Kernel
module.Find common types/functions like:
- at_exit
- open
- Integer
- ...
How to read API Docs
Ruby documentation can be difficult to browse.
In order to determine the scope you must browse:
- class inheritance hierarchy
- includes/extends
- parent-hierarchy's includes/extends
- includes/extends parent hierarchy
Try using ruby pry instead:
ls
(show methods/attrs by namespace/class)show-source -ld <method>
(show docs/sourcecode)