>
>>
>> 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
$
```
> >>
> >> \('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\)\.\.\.
>
I don't know if it's your English that's unclear, or that you'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't have to translate as you go\. Also, don't be
afraid to take your time\. I spend ages writing all these emails \(and I'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'>···</summary>
On 12 August 2016 at 22:06, A Berger <aberger7890@gmail\.com> wrote:
> 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/
</details>