Ruby equivalent for Javascript's 'console.log(a,b)' - Stack Overflow

In JavaScript, console.log works like this:$ node> console.log(1,2) ; console.log(3,4)1 23 4In Ru

In JavaScript, console.log works like this:

$ node
> console.log(1,2) ; console.log(3,4)
1 2
3 4

In Ruby, I want to output the value of a, followed by a space, then the value of b, then following that a newline. Neither of these are satisfactory:

$ irb
2.4.1 :001 > print 1,2 ; print 3,4
1234 => nil 
2.4.1 :002 > puts 1,2 ; puts 3,4
1
2
3
4
 => nil 

Am I out of luck without rolling my own?

In JavaScript, console.log works like this:

$ node
> console.log(1,2) ; console.log(3,4)
1 2
3 4

In Ruby, I want to output the value of a, followed by a space, then the value of b, then following that a newline. Neither of these are satisfactory:

$ irb
2.4.1 :001 > print 1,2 ; print 3,4
1234 => nil 
2.4.1 :002 > puts 1,2 ; puts 3,4
1
2
3
4
 => nil 

Am I out of luck without rolling my own?

Share Improve this question edited Oct 20, 2017 at 7:33 Stefan 114k14 gold badges156 silver badges230 bronze badges asked Oct 19, 2017 at 22:32 PurplejacketPurplejacket 2,1482 gold badges29 silver badges48 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

According to the doc, you have to override field separator and record separator.

$, = " "    # field separator
$\ = "\n"   # record separator
print 3, 5
3 5
nil

The equivalent would be

printf "%s %s\n", 1, 2

or

puts [1, 2].join(" ")

But you try to emulate javascript to show the contents of two variables on one line, I don't know about javascript but in Ruby two variables separated by a ma is an array. The real Ruby equivalent would be

p [1, 2]

or since you use irb

[1, 2]

In console.log you also see the line at which this code is executed, you can do that with __LINE__. A very simple debugging you can achieve while executing a script is like this

var = "content"
[__FILE__, __LINE__, "var", var]

Like this you get the name of the file where the line resides in, the line number, the name of the variable and the contents. Call it a poor mans debugger or console.log if you like.

Ruby equivalent for Javascript's console.log

Conceptually, Ruby's Logger class es close:

require 'logger'
logger = Logger.new(STDOUT)

logger.info [1, 2]
logger.info [3, 4]

but its default output is quite different:

I, [2017-10-20T09:29:16.372886 #13107]  INFO -- : [1, 2]
I, [2017-10-20T09:29:16.372966 #13107]  INFO -- : [3, 4]

You could write a custom formatter:

class SimpleFormatter
  def call(severity, time, progname, msg)
    "%s\n" % msg2str(msg)
  end

  def msg2str(msg)
    case msg
    when String then msg
    when Array  then msg.map(&:inspect).join(' ')
    else msg.inspect
    end
  end
end

Usage:

require 'logger'
logger = Logger.new(STDOUT, formatter: SimpleFormatter.new)

logger.info [1, 2]
logger.info [3, 4]

Output:

1 2
3 4

You could also take severity into account to colorize your output accordingly (so logger.warn prints in yellow, logger.error prints in red, etc.)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744992409a4604983.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信