views:

84

answers:

2

Hi I have a strange situation here:

# Is there a semantic difference between these 2 in ruby?


class MyClass < ClassThatExtendsActiveRecordBase

...

# code snippet A:

  def image_width(size); self.image.width(size); end

# vs code snippet B

  def image_width(size)
    self.image.width(size)
  end

end

I have a situation where code snippet A didn't appear to be recognised as a function but when I modify A by substituting semicolons with return chars to produce B the methods are recognised (this is ruby 1.8.7)

Is anyone able to help me understand what is happening that this occurs?

+2  A: 

Is there a semantic difference between these 2 in ruby?

There shouldn't be. And there isn't, at least with the versions I have tested:

  • YARV 1.9.2 Snapshot ruby 1.9.2dev (2010-01-02 trunk 26229) [i686-linux]
  • YARV 1.9.1p243 ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32]
  • MRI 1.8.7p174 ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-linux]
  • MRI 1.8.6p383 ruby 1.8.6 (2009-08-04 patchlevel 383) [i386-mingw32]
  • JRuby 1.5 Snapshot (1.9 mode) jruby 1.5.0.dev (ruby 1.9.2dev trunk 24787) (2009-11-02 6586) (Java HotSpot(TM) Client VM 1.7.0-ea) [x86-java]
  • JRuby 1.5 Snapshot (1.8 mode) jruby 1.5.0.dev (ruby 1.8.7 patchlevel 174) (2009-11-02 6586) (Java HotSpot(TM) Client VM 1.7.0-ea) [x86-java]
  • JRuby 1.4.0 (1.9 mode) jruby 1.4.0 (ruby 1.9.2dev trunk 24787) (2009-11-02 69fbfa3) (Java HotSpot(TM) Client VM 1.7.0-ea) [x86-java]
  • JRuby 1.4.0 (1.8 mode) jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) Client VM 1.7.0-ea) [x86-java]
  • JRuby 1.3.1 (1.9 mode) jruby 1.3.1 (ruby 1.8.6p287) (2009-06-15 2fd6c3d) (Java HotSpot(TM) Client VM 1.6.0_14) [i386-java]
  • JRuby 1.3.1 (1.8 mode) jruby 1.3.1 (ruby 1.9.1p0) (2009-06-15 2fd6c3d) (Java HotSpot(TM) Client VM 1.6.0_14) [i386-java]
  • IronRuby IronRuby 0.9.1.0 on .NET 4.0.0.0
  • IronRuby IronRuby 0.9.2.0 on .NET 2.0.0.0

Since the code example you posted is incomplete and doesn't even parse correctly, I used a slightly modified version to test:

class C < Object
  def semicolon(arg); self.foo(arg); end

  def newlines(arg)
    self.foo(arg)
  end

  def really_short(arg) foo(arg) end

  def foo(arg) arg end
end

require 'test/unit'
class TestMethodDefinition < Test::Unit::TestCase
  def test_that_semicolons_work
    assert_equal :foo, C.new.semicolon(:foo)
  end
  def test_that_newlines_work
    assert_equal :foo, C.new.newlines(:foo)
  end
  def test_that_really_short_definitions_work
    assert_equal :foo, C.new.really_short(:foo)
  end
end

You wrote

[...] didn't appear to be recognised as a function

What do you mean by that? There is no such thing as a function Ruby, so the fact that functions don't work shouldn't really be surprising.

Also, what do you mean by "recognised"? This word has a very specific meaning in programming, but you don't appear to be using it with that meaning.

And lastly, what does it mean that it "appears" to not work? Does it or doesn't it work?

Could you provide a complete and minimal testcase that exhibits the behavior you are seeing? Also, please describe:

  • what is the behavior you are expecting
  • why you think that behavior should occur
  • what is the behavior you are actually observing
  • what are the precise error messages you are getting

At the moment, it looks like a very subtle bug in your specific installation of Ruby.

Jörg W Mittag
Thanks Jörg you're awesomely helpful. I'm having problems finding a minimal test case -- when I strip it out like you do, it goes away. If I manage I'll follow-up.
Julian
+1  A: 

There are certain cases in which newlines are simply ignored. For example

1 + 
2

is the same as 1 + 2. In those cases using a semicolon is not equivalent as it would result in a syntax error. In all other cases it is equivalent.

sepp2k