Is there equivalent for PHP's print_r in Ruby / Rails?

前端 未结 8 752
鱼传尺愫
鱼传尺愫 2020-12-08 03:17

In PHP you can do:

print_r($var) or vardump($var)

which prints \"human-readible\" information about variable.

Is there equ

相关标签:
8条回答
  • 2020-12-08 03:39

    Guess I'm a little late to this, but what about logger.info [debug|warning]? Use this from Controllers and Models. It will show up in your log files (development.log when in dev mode); and the above mentioned <%= debug("str: " + str) %> for views.

    These aren't exact answers to your questions but you can also use script/console to load your rails app in to an interactive session.

    Lastly, you can place debugger in a line of your rails application and the browser will "hang" when your app executes this line and you'll be able to be in a debug session from the exact line your placed your debugger in the source code.

    0 讨论(0)
  • 2020-12-08 03:43

    There's the method inspect which helps. Sometimes calling the to_s method on an object will help (to_s returns a string representation of the object). You can also query methods, local_variables, class_variables, instance_variables, constants and global_variables.

    p ['Hello',"G'day",'Bonjour','Hola'].inspect
    # >> "[\"Hello\", \"G'day\", \"Bonjour\", \"Hola\"]"
    
    p ['Hello',"G'day",'Bonjour','Hola'].to_s
    # >> "HelloG'dayBonjourHola"
    
    p Array.new.methods
    # >> ["select", "[]=", "inspect", "compact"...]
    
    monkey = 'baboon'
    p local_variables
    # >> ["monkey"]
    
    class Something
      def initialize
        @x, @y = 'foo', 'bar'
        @@class_variable = 'gorilla'
      end
    end
    
    p Something.class_variables
    # >> ["@@class_variable"]
    
    s = Something.new
    p s.instance_variables
    # >> ["@x", "@y"]
    
    p IO.constants
    # >> ["TRUNC", "SEEK_END", "LOCK_SH"...]
    
    p global_variables
    # >> ["$-d", "$\"", "$$", "$<", "$_", "$-K"...]
    
    0 讨论(0)
提交回复
热议问题