Skip to content

Instantly share code, notes, and snippets.

@schanjr
Last active May 2, 2021 11:36
Show Gist options
  • Save schanjr/018f2b28dbd4f8c0c8903a7fb992a077 to your computer and use it in GitHub Desktop.
Save schanjr/018f2b28dbd4f8c0c8903a7fb992a077 to your computer and use it in GitHub Desktop.
Ruby Debugging Useful Scripts - Still googling dumb ruby stuff, this is my compiling cheatsheet.

edited with... https://hackmd.io/PdE46uT6TQO-WD6CcX1fXA?

Creating breakpoints

gem install pry

require 'pry'

def crypticMethod(env)
   # some code
   binding.pry
   puts "making sure binding.pry is not the last execution"
   # Note that binding.pry inserted binding.pry should not be the last line of a method. 
end 

Check where the method is declared

require 'json'
JSON.method(:parse).source_location
=> ["/Users/chanst/.rvm/rubies/jruby-9.2.9.0/lib/ruby/stdlib/json/common.rb", 155] 

Inspect what methods are defined of a given object

String.new.methods

Instance Variables

Inspect what Instance Variable Names exists

Object.new.instance_variables

=> # [:@water]

GET/SET the value of an instance variable

Object.new.instance_variables.get(:@water)
Object.new.instance_variables.set(:@water, "Poland Springs")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment