Puts method

Hi
How can I see where puts is defined?

x.puts should be available in Ruby!
("Why not?")

=> Is the class Object best for
def puts ... end
so that also x.puts works ?
(for x=anything)

Problem:
3.puts(4) # should rise an error!
How to test if there is an obj given
(obj.puts instead of puts) ?

('self' depends on class where puts is called from, so "if self==" doesnt
seem to be useful?)

Thanks
Berg

Berg,

Please stop trolling.

x.puts should be available in Ruby! ("Why not?")

What for???

If you do not like Ruby the way it is,
maybe you should create your own language?

Regards,
Marcus

···

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Hi
How can I see where puts is defined?

Module: Kernel (Ruby 2.3.0)

x.puts should be available in Ruby!
("Why not?")

​It is, for any 'x' that makes sense to define a
#puts
method. E.g.
IO

=> Is the class Object best for
def puts ... end
so that also x.puts works ?
(for x=anything)

​Sure, why not? But it's already defined on the Kernel module, which is
included by Object. The thing is, it's private (so you can't call it with a
'.')​

Problem:
3.puts(4) # should rise an error!

​Why? You haven't told us what your weird `
3.puts
` should do in the first place.​

How to test if there is an obj given
(obj.puts instead of puts) ?

​Er... dude, that's function parameters. Look up how to define/not define
them. Basic language stuff here.​

('self' depends on class where puts is called from, so "if self==" doesnt
seem to be useful?)

​I think I know what you're trying to say, but you're really not saying it
well. You should write things first in your preferred/native language,
fully, without using local idioms that might not carry over well; then
translate to English.​

Thanks
Berg

​Cheers​

···

On 12 August 2016 at 17:58, A Berger <aberger7890@gmail.com> wrote:
--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

Hi
How can I see where puts is defined?

Module: Kernel (Ruby 2.3.0)

I meant which Ruby method tells me that?

x.puts should be available in Ruby!
("Why not?")

​It is, for any 'x' that makes sense to define a
#puts
method. E.g.
IO
Class: IO (Ruby 2.3.0)

=> Is the class Object best for
def puts ... end
so that also x.puts works ?
(for x=anything)

​Sure, why not? But it's already defined on the Kernel module, which is

included by Object. The thing is, it's private (so you can't call it with a
'.')​

Can you make a private method public, without redefining it (without
knowing its content)?

Problem:
3.my_puts(4) # should rise an error!

​Why? You haven't told us what your weird
3.puts
should do in the first place.​

Ok
it can be called with obj.my_puts OR my_puts(obj)

How to test if there is an obj given
(obj.puts instead of puts) ?

​Er... dude, that's function parameters. Look up how to define/not define

them. Basic language stuff here.​

No?, I want to look at the object ( the method being sent to ).
How can I see if there is an (user given) obj for obj.my_puts

('self' depends on class where puts is called from, so "if self=="

doesnt seem to be useful?)

​I think I know what you're trying to say, but you're really not saying

it well. You should write things first in your preferred/native language,
fully, without using local idioms that might not carry over well; then
translate to English.​

Hoping it could be understood now.
Seems my English must be brushed up (or fully renewed)...

···

Am 12.08.2016 12:14 schrieb "Matthew Kerwin" <matthew@kerwin.net.au>:

On 12 August 2016 at 17:58, A Berger <aberger7890@gmail.com> wrote:

Thanks
Berg

​Cheers​
--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi Marcus,
as you can see, all these threads come to an interesting discussion,
special thanks for the comprehensive answers from Matthew!

Yes, in my own language I would have done many things differently, but I
dont underestimate the effort building such a language. - Ideas are there
many...
And surely I had never created such XPath... and html.
Also the gem-command could be nicer. - But I highly regard that we have it,
especially Ruby! :slight_smile:

--- That leads to the next question...
- But that would be too much for you :wink:
Berg

Berg,

Please stop trolling.

x.puts should be available in Ruby! ("Why not?")

What for???

If you do not like Ruby the way it is,
maybe you should create your own language?

Regards,
Marcus

···

Am 12.08.2016 10:13 schrieb <sto.mar@web.de>:

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

>
>>
>> Hi
>> How can I see where puts is defined?
> ​Module: Kernel (Ruby 2.3.0)

I meant which Ruby method tells me that?

​```
$ ruby -e 'puts method(:puts).owner'
Kernel
$


> >>
> >> x.puts should be available in Ruby!
> >> ("Why not?")
> >
> > ​It is, for any 'x' that makes sense to define a
> > #puts
> > method. E.g.
> > IO
> > http://ruby-doc.org/core-2.3.0/IO.html#method-i-puts
> > ​
> >
> >>
> >> => Is the  class Object   best for
> >> def puts ... end
> >> so that also x.puts works ?
> >> (for x=anything)
> >
> > ​Sure, why not? But it's already defined on the Kernel module, which is
> included by Object. The thing is, it's private (so you can't call it with a
> '.')​
>
> Can you make a private method public, without redefining it (without
> knowing its content)?
>

​Not exactly, but you can work around it.

$ cat test.rb

class Foo
  def foo
    42
  end
  private :foo
end

begin
  p Foo.new.foo
rescue => e
  puts e
end

class Foo
  alias :old_foo :foo
  def foo
    old_foo
  end
end

p Foo.new.foo

$ ruby test.rb
private method `foo' called for #<Foo:0x00000000f79e00>
42
$


I wouldn't, though. Methods are usually made private for a reason.​

> >> Problem:
> >> 3.my_puts(4) # should rise an error!
> >
> > ​Why? You haven't told us what your weird
> > 3.puts
> >  should do in the first place.​
>
> Ok
> it can be called with obj.my_puts OR my_puts(obj)
>

​You can sort of raise that error, using a couple of different heuristics,
but they depend heavily on your exact context. However I wouldn't even
bother doing that; if it can be invoked as a method or as a function, why
not just define a method *and *a function? In this case, since the "puts"
function already exists, why not define a (new) method that does what you
want?

I don't have a problem with the idea of defining something like a
"serialize" method on one of the root classes, for example:

​```
$ cat serialise.rb
module Kernel
  def serialize to: $stdout
    to.puts self
  end
end

123.serialize
"hello".serialize to: $stderr

$ ruby serialise.rb
123
hello
$

>>
>> How to test if there is an obj given
>> (obj.puts instead of puts) ?
>
> ​Er... dude, that's function parameters. Look up how to define/not
define them. Basic language stuff here.​

No?, I want to look at the object ( the method being sent to ).
How can I see if there is an (user given) obj for obj.my_puts

​I think I understand, you want to know if the method was invoked with an
explicit receiver. Why not approach it the other way? If no parameters were
passed to your method, then assume it was called with a receiver:

$ cat test3.rb

module Kernel
  def baz *args
    case n = args.length
    when 0
      "called with receiver #{inspect}"
    when 1
      "called with parameter #{args[0].inspect}"
    else
      raise ArgumentError, "wrong number of arguments (#{n} for 0..1)"
    end
  end
end

obj = Object.new

puts obj.baz
puts baz(obj)

$ ruby test3.rb
called with receiver #<Object:0x00000000c46a30>
called with parameter #<Object:0x00000000c46a30>
$

In other words, let 'self' be the default parameter:

$ cat test4.rb

module Kernel
  def baz arg=self
    puts arg
  end
end

obj = "hello"

obj.baz
baz(obj)

$ ruby test4.rb
hello
hello
$
```​

> &gt;&gt;
> &gt;&gt; \(&#39;self&#39; depends on class where puts is called from, so &quot;if self==&quot;
> doesnt seem to be useful?\)
> &gt;
> &gt; ​I think I know what you&#39;re trying to say, but you&#39;re really not saying
> it well\. You should write things first in your preferred/native language,
> fully, without using local idioms that might not carry over well; then
> translate to English\.​
>
> Hoping it could be understood now\.
> Seems my English must be brushed up \(or fully renewed\)\.\.\.
>

​I don&#39;t know if it&#39;s your English that&#39;s unclear, or that you&#39;re not
thinking it through thoroughly and expressing it fully\. I was just
suggesting you write in your mother tongue because it might help your
thought processes if you don&#39;t have to translate as you go\. Also, don&#39;t be
afraid to take your time\. I spend ages writing all these emails \(and I&#39;m a
native English speaker\) \- thinking, writing, reading, rewriting, etc\. to
make sure my email says what I want it to say\.​

<details class='elided'>
<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>

On 12 August 2016 at 22:06, A Berger &lt;aberger7890@gmail\.com&gt; wrote:
> Am 12\.08\.2016 12:14 schrieb &quot;Matthew Kerwin&quot; &lt;matthew@kerwin\.net\.au&gt;:
> &gt; On 12 August 2016 at 17:58, A Berger &lt;aberger7890@gmail\.com&gt; wrote:

> &gt;&gt; Thanks
> &gt;&gt; Berg
> &gt;
> &gt; ​Cheers​
> &gt;
>

\-\- 
&nbsp;&nbsp;Matthew Kerwin
&nbsp;&nbsp;http://matthew.kerwin.net.au/

</details>

    Can you make a private method public, without redefining it (without
    knowing its content)?

​Not exactly, but you can work around it.

Yes you can, see below.

$ cat test.rb 

class Foo
  def foo
    42
  end
  private :foo
end

begin
  p Foo.new.foo
rescue => e
  puts e
end

class Foo
  alias :old_foo :foo
  def foo
    old_foo
  end
end

you could instead do:

class Foo
&nbsp;&nbsp;public :foo
end
\`\`\`

Regards,
Marcus

<details class='elided'>
<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>

Am 12\.08\.2016 um 15:06 schrieb Matthew Kerwin:

\-\- 
GitHub: https://github.com/stomar/
PGP:    0x6B3A101A

</details>

No, often they lead to discussions about what you really
meant with your question(s). See e.g. the "Nikogiri" thread
were it's still unclear whether you need something like
nokogiri or rather something like mechanize.

And often you do not even bother to clarify your meaning,
when asked for it, or you apparently do not care to read
resources that are pointed out to you, or complain about
strangely behaving code but forget to mention that you
are not working with MRI, or still ask questions that
can easily be answered by a short web search.

This might sound a little harsh, but IMO the noise level
in this group has drastically increased in the last months
due to these kinds of posts. I'm beginning to consider
this trolling.

Leam Hall posted some "rules" for good posts a couple
of weeks ago, here they are again:

1. Specify platform and Ruby versions.
Also, other tools or gems being used.

2. Specify the question. The more specific, the better.

3. Show some code. That does three things; it often points out the error
as you type, it shows people you're put some effort into it, and it
gives an explicit demonstration of your code.

4. Be patient and thoughtful. Like I said, there are a lot of smart
folks here. Most of they have day jobs and families, not sure if anyone
is paid to just sit on the e-mail list and answer questions.

I think you would get better answers if you would try to follow
these hints.

Please keep in mind that I often _did_ try to help you with
your questions, so I hope you won't get this the wrong way.

Regards,
Marcus

···

Am 12.08.2016 um 15:15 schrieb A Berger:

as you can see, all these threads come to an interesting discussion,
special thanks for the comprehensive answers from Matthew!

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

​Actually, I was overthinking it. You can do the following:

$ cat test.rb

class Foo
  def foo
    :foo
  end
  private :foo
end

begin
  p Foo.new.foo
rescue => e
  puts e
end

class Foo
  public :foo
end

p Foo.new.foo

$ ruby test.rb
private method `foo' called for #<Foo:0x0000000112ea70>
:foo
$

Cheers​

···

On 12 August 2016 at 23:06, Matthew Kerwin <matthew@kerwin.net.au> wrote:

Can you make a private method public, without redefining it (without

knowing its content)?

​Not exactly, but you can work around it.

--
  Matthew Kerwin
  http://matthew.kerwin.net.au/

I would include on this rules:
5. Google your question(s) before asking
6. Read the errors, they are there for a reason.

···

El 12/08/2016 15:43, <sto.mar@web.de> escribió:

Am 12.08.2016 um 15:15 schrieb A Berger:
> as you can see, all these threads come to an interesting discussion,
> special thanks for the comprehensive answers from Matthew!

No, often they lead to discussions about what you really
meant with your question(s). See e.g. the "Nikogiri" thread
were it's still unclear whether you need something like
nokogiri or rather something like mechanize.

And often you do not even bother to clarify your meaning,
when asked for it, or you apparently do not care to read
resources that are pointed out to you, or complain about
strangely behaving code but forget to mention that you
are not working with MRI, or still ask questions that
can easily be answered by a short web search.

This might sound a little harsh, but IMO the noise level
in this group has drastically increased in the last months
due to these kinds of posts. I'm beginning to consider
this trolling.

Leam Hall posted some "rules" for good posts a couple
of weeks ago, here they are again:

1. Specify platform and Ruby versions.
Also, other tools or gems being used.

2. Specify the question. The more specific, the better.

3. Show some code. That does three things; it often points out the error
as you type, it shows people you're put some effort into it, and it
gives an explicit demonstration of your code.

4. Be patient and thoughtful. Like I said, there are a lot of smart
folks here. Most of they have day jobs and families, not sure if anyone
is paid to just sit on the e-mail list and answer questions.

I think you would get better answers if you would try to follow
these hints.

Please keep in mind that I often _did_ try to help you with
your questions, so I hope you won't get this the wrong way.

Regards,
Marcus

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Oops, I often do not follow this rule -- I hope
it's also ok to duckduckgo the question :wink:

Regards,
Marcus

···

Am 12.08.2016 um 16:14 schrieb Felipe Tavares:

5. Google your question(s) before asking

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A