Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7217 > unrolled thread

"Put" in Ruby

Started byCai Gengyang <gengyangcai@gmail.com>
First post2016-04-15 08:23 -0700
Last post2016-04-16 01:39 +0000
Articles 7 — 4 participants

Back to article view | Back to comp.lang.ruby


Contents

  "Put" in Ruby Cai Gengyang <gengyangcai@gmail.com> - 2016-04-15 08:23 -0700
    Re: "Put" in Ruby Sebastian Christ <rudolfo.christ@gmail.com> - 2016-04-15 20:57 +0200
      Re: "Put" in Ruby Robert Klemme <shortcutter@googlemail.com> - 2016-04-16 01:45 +0200
        Re: "Put" in Ruby Sebastian Christ <rudolfo.christ@gmail.com> - 2016-04-16 11:52 +0200
          Re: "Put" in Ruby Robert Klemme <shortcutter@googlemail.com> - 2016-04-16 13:40 +0200
    Re: "Put" in Ruby Robert Klemme <shortcutter@googlemail.com> - 2016-04-16 01:50 +0200
    Re: "Put" in Ruby Kaz Kylheku <545-066-4921@kylheku.com> - 2016-04-16 01:39 +0000

#7217 — "Put" in Ruby

FromCai Gengyang <gengyangcai@gmail.com>
Date2016-04-15 08:23 -0700
Subject"Put" in Ruby
Message-ID<d720c9a9-3943-45d9-83a1-ff3cf224aaef@googlegroups.com>
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 ...

[toc] | [next] | [standalone]


#7218

FromSebastian Christ <rudolfo.christ@gmail.com>
Date2016-04-15 20:57 +0200
Message-ID<m2inziaeew.fsf@gmail.com>
In reply to#7217
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

[toc] | [prev] | [next] | [standalone]


#7219

FromRobert Klemme <shortcutter@googlemail.com>
Date2016-04-16 01:45 +0200
Message-ID<dndcohFd463U1@mid.individual.net>
In reply to#7218
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/

[toc] | [prev] | [next] | [standalone]


#7222

FromSebastian Christ <rudolfo.christ@gmail.com>
Date2016-04-16 11:52 +0200
Message-ID<m237qlanjv.fsf@gmail.com>
In reply to#7219
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

[toc] | [prev] | [next] | [standalone]


#7223

FromRobert Klemme <shortcutter@googlemail.com>
Date2016-04-16 13:40 +0200
Message-ID<dneml0FmlqjU1@mid.individual.net>
In reply to#7222
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/

[toc] | [prev] | [next] | [standalone]


#7220

FromRobert Klemme <shortcutter@googlemail.com>
Date2016-04-16 01:50 +0200
Message-ID<dndd1lFd67gU1@mid.individual.net>
In reply to#7217
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/

[toc] | [prev] | [next] | [standalone]


#7221

FromKaz Kylheku <545-066-4921@kylheku.com>
Date2016-04-16 01:39 +0000
Message-ID<20160415183500.347@kylheku.com>
In reply to#7217
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!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web