3

Below is a small screenshot from within RubyMine 3.1. I am just starting to learn Ruby. The code here is from the Presenter-First MVP C# code generator over at atomicobject.com.

I am using this project along with a book to learn Ruby. The documentation for puts shows that it expects at least one parameter. Yet this code appears "somewhat legal" for two reasons:

  1. The code appears to work fine when I step thru it via the debugger.
  2. Searching online, and even here at SO, shows that puts w/o arguments creates a newline.

However, is it bad practice to do this (hence the RubyMine warning)? The code I am looking at is from 2006. I'm running it with Ruby 1.9.2 if that matters any.

enter image description here

Community
  • 1
  • 1
Matt
  • 14,353
  • 5
  • 53
  • 65

2 Answers2

3

This is perfectly fine, as puts provides 'default' value for the first parameter:

def puts(obj='', *arg)

As for RubyMine, it doesn't show any errors for me. May it happen that you define method puts somewhere else in your code? You can cmd+click on it, to get to the definition.

Anyway, if you're able to reproduce problem in a clean new project, you can freely submit a bug report to JetBrains.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
  • @Nikita Rybak, When I click "Go To Definition" I Get this: `def puts(obj, *smth)` in kernel.rb. This kernal.rb method is a stub for indexing and the comment at the top of the file says: "This is a machine generated stub using stdlib-doc for module Kernel Ruby sources used: Ruby-1.9.1 p0 Created on Wed Jun 17 12:55:08 +0400 2009 by IntelliJ Ruby Stubs Generator." – Matt Feb 16 '11 at 14:29
  • @Matt That's interesting... I do think there's a default assignment somewhere, as invoking methods without it (e.g., `print`) will make `nil` to appear for me. – Nikita Rybak Feb 16 '11 at 14:40
  • @Matt If you have spare time, you can still open a ticket at jetbrains and see what they say. – Nikita Rybak Feb 16 '11 at 14:41
  • @Nikita, I'll try to recreate the issue with an empty project, and if successful I'll open a ticket. Thanks for your help. – Matt Feb 16 '11 at 14:43
  • @Nikita, They plan to fix this in 3.1.1: http://youtrack.jetbrains.net/issue/RUBY-7846 – Matt Feb 16 '11 at 14:58
1

No, it can be helpful to create the physical line break in your source as well as the output, and like you have seen already, puts is perfectly capable of accepting zero arguments.

Personally, if I'm creating a multi-line output I prefer to use here-doc syntax.

wersimmon
  • 2,809
  • 3
  • 22
  • 35