Up arrow to previously executed code in irb
Use Opt+Enter to add a new line without executing
None of these raise by default:
system "exit 1"
`exit 1`
%x(exit 1)
Instead, use:
system "exit 1", exception: true
The symbol to proc functionality assumes that the symbol is a member
of the
calling instance. To call a pure function, use
my_array.map(&method(:my_fn))
.
For example:
def add_one(n)
return n + 1
end
[1,2,3].map &method(:add_one)
Either use:
require "debug"
:: Add breakpoint in source with `debugger`
Or add binding.irb
in source, no require
necessary.
I prefer the former, because stepping (n
) and continuing
(c
) through the source works.
Move down: Tab
Move up: Shift-Tab
Start irb with irb --noautocomplete
or add IRB.conf[:USE_AUTOCOMPLETE] = false
to
~/.irbrc
myfile.txt.erb
, use placeholders like
<%= my_variable %>
generator.rb
: require "erb"
my_variable = "hello world"
result = ERB.new(File.read("myfile.txt.erb")).result(binding)
File.write("myfile.txt", result)
ruby generator.rb
, and observe
myfile.txt
on disk.rvm get stable
rvm --default use X.Y.Z
rvm alias create myruby ruby-2.4.1
rvm use myruby
https://gist.github.com/dvliman/10402435
See where bundler finds a specific gem:
bundle show <gemname>
http://ruby-doc.org/stdlib-2.2.0/libdoc/ripper/rdoc/Ripper.html
List methods on an instance that are defined outside of
Object
:
class Object
def local_methods
(methods - Object.instance_methods).sort
end
end
To see why ~/.irbrc is not working properly:
ruby ~/.irbrc
/private/etc/irbrc
holds the default irb config, can be
overwritten by creating a ~/.irbrc
file.
See the rerun gem
$ rerun --background --no-growl rackup service.ru
Pry is really sweet.
$ pry
> cd FileUtils
> ls
> show-method mkdir
require 'active_support/time'
I added this here:
https://stackoverflow.com/a/14725405/143447
Given a starting value, see what method will give the desired result
with what_methods
.
E.g.
require 'what_methods'
10.what? "10" #=> 10.to_s, 10.inspect
require "wirble"
IRB.CurrentContext.prompt_i = Wirble::Colorize.colorize_string(IRB.CurrentContext.prompt_i, :red)
gem env
rvm implode
After installing rvm, I was surprised that which rvm
exited with 1.
rvm
is a function that’s sourced into my shell.
Use command -v rvm
to see if my shell can find it.
Use bash --debugger -cl "declare -F rvm"
to find where the
function is defined.
The output will contain the definition file on disk and the source line
number.
Do not add require "rubygems"
to code.
Start ruby with ruby -rubygems
, or irb with
irb -rrubygems
, or add this to
~/.bash_profile
:
export RUBYOPT="rubygems"
source: https://tomayko.com/blog/2009/require-rubygems-antipattern
Here is an interesting difference:
irb> a = "string with \n new line"
irb> a =~ /(.*)/
irb> $1
=> "string with "
irb> a =~ /([^"]*)/
irb> $1
=> "string with \n new line"
The .* does not match new lines, but negated class does
class A
@greeting = "hello"
class << self
attr_reader :greeting
end
end
class B < A
@greeting = "hola"
end
A.greeting # => "hello"
B.greeting # => "hola"
ObjectSpace.each_object(Class) do |cls|
if cls.ancestors.include? Exception
puts cls
end
end
irb> self.class.constants.select { |c| c =~ /needle/i }
object.message
is the same as
object.send(:message)
For example, 5.send :to_s
a = "[0-9]+"
b = /#{a}/
month = 5
month.to_s.gsub(/^(\d)$/, '0\1')
month = ((month - 1 + N ) % 12) + 1
require 'benchmark'
include Benchmark
bm(10) do |test|
test.report("result 1:") do
# test something
end
test.report("result 2:") do
# test something else
end
end
Gotcha:
array = Array.new(2,[])
array[0][0] = "x"
array
# => [["x"], ["x"]]
# Both subarrays are filled with "x" (it's really one array)
Use:
array = Array.new(2) { [] }
array[0][0] = "x"
array
# => [["x"], []]
These accomplish the same thing:
class Test
attr_accessor :one, :two, :three
def initialize(one, two, three)
@one = one
@two = two
@three = three
end
end
Test.new(1,2,3)
and
class Test
attr_accessor :one, :two, :three
def initialize(&block)
instance_eval &block
end
end
Test.new {self.one = 1; self.two = 2; self.three = 3}
Loop through escape sequences in IRB to pick a color:
(30..50).each do |x|
puts x
print "\e[#{x}mTEST\e[0m\n"
end
Time.now.isdst