• "Put" in Ruby

    From Cai Gengyang@21:1/5 to All on Fri Apr 15 08:23:10 2016
    An examination of "Put" in Ruby :

    # this one is like your scripts with ARGV
    def print_two(*args)
    arg1, arg2 = args
    puts "arg1: #{arg1}, arg2: #{arg2}"
    end

    # ok, that *args is actually pointless, we can just do this
    def print_two_again(arg1, arg2)
    puts "arg1: #{arg1}, arg2: #{arg2}"
    end

    # this just takes one argument
    def print_one(arg1)
    puts "arg1: #{arg1}"
    end

    # this one takes no arguments
    def print_none()
    puts "I got nothin'."
    end


    print_two("Zed","Shaw")
    print_two_again("Zed","Shaw")
    print_one("First!")
    print_none()


    First off, puts is not a function. It's sole purpose is to have a side-effect (printing something to the console), whereas functions cannot have side-effects ... that's the definition of "function", after all.

    Ruby doesn't have functions. It only has methods. Thus, puts is a method.

    Is this true ? So basically, 'puts' is just a "thing" to enable printing something to the console ...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sebastian Christ@21:1/5 to Cai Gengyang on Fri Apr 15 20:57:11 2016
    On 2016-04-15 8:23, Cai Gengyang <gengyangcai@gmail.com> wrote:
    First off, puts is not a function. It's sole purpose is to have a side-effect (printing something to the console), whereas functions
    cannot have side-effects ... that's the definition of "function",
    after all.

    Ruby doesn't have functions. It only has methods. Thus, puts is a method.

    Is this true ? So basically, 'puts' is just a "thing" to enable printing something to the console ...

    irb(main):001:0> RUBY_VERSION
    "2.3.0"
    irb(main):002:0> puts.class

    NilClass
    irb(main):003:0>

    Looks like `puts' is an instance of NilClass. And therefore an object.

    Regards,
    Sebastian

    --
    Sebastian (Rudolfo) Christ
    http://rudolfochrist.github.io
    GPG Fingerprint: 306D 8FD3 DFB6 4E44 5061
    CE71 6407 D6F8 2AC5 55DD

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Klemme@21:1/5 to Sebastian Christ on Sat Apr 16 01:45:19 2016
    On 15.04.2016 20:57, Sebastian Christ wrote:

    irb(main):001:0> RUBY_VERSION
    "2.3.0"
    irb(main):002:0> puts.class

    NilClass
    irb(main):003:0>

    Looks like `puts' is an instance of NilClass. And therefore an object.

    This is ridiculous - or an attempt at humor. You invoke .class on the
    result of invoking puts - not on puts.

    irb(main):001:0> method :puts
    #<Method: Object(Kernel)#puts>
    irb(main):002:0> _.owner
    Kernel

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Klemme@21:1/5 to Cai Gengyang on Sat Apr 16 01:50:12 2016
    On 15.04.2016 17:23, Cai Gengyang wrote:

    First off, puts is not a function. It's sole purpose is to have a
    side-effect (printing something to the console), whereas functions
    cannot have side-effects ... that's the definition of "function",
    after all.

    More precisely this is _one_ definition of function. Other context are
    more liberal and will include functions with side effects.

    Ruby doesn't have functions. It only has methods. Thus, puts is a
    method.

    Is this true ?

    If you apply a strict definition, yes. Every method can have side
    effects - but it does not need to. There is no way to declare a "thing"
    in Ruby to be a function without side effects.

    So basically, 'puts' is just a "thing" to enable
    printing something to the console ...

    It is a method with the side effect of writing a character
    representation of its arguments to whatever file descriptor 0 (usually
    called "stdout") points to. Additionally there are some formatting
    tricks, i.e. newlines will be inserted between output of arguments.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Cai Gengyang on Sat Apr 16 01:39:23 2016
    On 2016-04-15, Cai Gengyang <gengyangcai@gmail.com> wrote:
    First off, puts is not a function. It's sole purpose is to have a
    side-effect (printing something to the console), whereas functions
    cannot have side-effects ... that's the definition of "function",
    after all.

    If you can't write and debug a function, might as well have an
    opinion on what it is and isn't!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sebastian Christ@21:1/5 to Robert Klemme on Sat Apr 16 11:52:04 2016
    On 2016-04-16 1:45, Robert Klemme <shortcutter@googlemail.com> wrote:
    This is ridiculous - or an attempt at humor. [Snip..]

    Perhaps a combination of both ;-)

    Regards,
    Sebastian

    --
    Sebastian (Rudolfo) Christ
    http://rudolfochrist.github.io
    GPG Fingerprint: 306D 8FD3 DFB6 4E44 5061
    CE71 6407 D6F8 2AC5 55DD

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Klemme@21:1/5 to Sebastian Christ on Sat Apr 16 13:40:15 2016
    On 16.04.2016 11:52, Sebastian Christ wrote:
    On 2016-04-16 1:45, Robert Klemme <shortcutter@googlemail.com> wrote:
    > This is ridiculous - or an attempt at humor. [Snip..]

    Perhaps a combination of both ;-)

    That was not an XOR. ;-)

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)