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

前端 未结 8 769
鱼传尺愫
鱼传尺愫 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: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"...]
    

提交回复
热议问题