classless methods

D

Dave Rose

what class does a classless independent method belong too?
another words if i just make an new irb session and type:

def widget(tidbit)
tidbit
end

a=1.0
puts widget(a)

b='string'

puts widget(b)

what class will the widget method belong too?
 
T

Trans

Dave said:
what class does a classless independent method belong too?
another words if i just make an new irb session and type:

def widget(tidbit)
tidbit
end

a=1.0
puts widget(a)

b='string'

puts widget(b)

what class will the widget method belong too?

Not Kernel, it becomes a private method of Object class.

T.
 
E

Eero Saynatkari

--5vb8QKeSi34BFwx0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

what class does a classless independent method belong too?
another words if i just make an new irb session and type:
=20
def widget(tidbit)
tidbit
end
=20
a=3D1.0
puts widget(a)
=20
b=3D'string'
=20
puts widget(b)
=20
what class will the widget method belong too?

Ruby can tell you.

def widget
# ...
end

p method('widget') # =3D> #<Method: Object#widget>

p Object.methods.include? 'widget' # =3D> true=20
p Object.ancestors # =3D> [Object, Kernel]
p Kernel.methods.include? 'widget' # =3D> true=20

--5vb8QKeSi34BFwx0
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFQj1Y7Nh7RM4TrhIRAuRxAKDCdjQYRNvXDEYX1Mj9VG9bonUGPgCgocMF
Prg9lfPt0xaky9ryqSn+rkQ=
=zLWP
-----END PGP SIGNATURE-----

--5vb8QKeSi34BFwx0--
 
J

Jason Merrill

And since Object is an ancestor of pretty much everything in ruby,
it's interesting to note that these methods are now defined for
practically everything in ruby:

irb(main):001:0> def say_hello
irb(main):002:1> "Hello"
irb(main):003:1> end
=> nil
irb(main):004:0> say_hello
=> "Hello"
irb(main):005:0> [1,3,5].say_hello
=> "Hello"
irb(main):006:0> Array.say_hello
=> "Hello"
irb(main):007:0> Hash.say_hello
=> "Hello"
irb(main):008:0>
 
M

matt neuburg

Jason Merrill said:
And since Object is an ancestor of pretty much everything in ruby,
it's interesting to note that these methods are now defined for
practically everything in ruby:

irb(main):001:0> def say_hello
irb(main):002:1> "Hello"
irb(main):003:1> end
=> nil
irb(main):004:0> say_hello
=> "Hello"
irb(main):005:0> [1,3,5].say_hello
=> "Hello"

But that's just a vagary of how irb works. You couldn't do that in a
real script. m.
 
H

Hugh Sasse

Jason Merrill said:
And since Object is an ancestor of pretty much everything in ruby,
it's interesting to note that these methods are now defined for
practically everything in ruby:

irb(main):001:0> def say_hello
irb(main):002:1> "Hello"
irb(main):003:1> end
=> nil
irb(main):004:0> say_hello
=> "Hello"
irb(main):005:0> [1,3,5].say_hello
=> "Hello"

But that's just a vagary of how irb works. You couldn't do that in a
real script. m.

Why not? say_hello is now a method of Object, an ancestor of [1,3,5].
Challenge: show us what the error is when it fails. :)

Hugh
 
M

matt neuburg

Hugh Sasse said:
Jason Merrill said:
And since Object is an ancestor of pretty much everything in ruby,
it's interesting to note that these methods are now defined for
practically everything in ruby:

irb(main):001:0> def say_hello
irb(main):002:1> "Hello"
irb(main):003:1> end
=> nil
irb(main):004:0> say_hello
=> "Hello"
irb(main):005:0> [1,3,5].say_hello
=> "Hello"

But that's just a vagary of how irb works. You couldn't do that in a
real script. m.

Why not? say_hello is now a method of Object, an ancestor of [1,3,5].
Challenge: show us what the error is when it fails. :)

matt-neuburgs-imac-g5:~ mattneub$ ruby

def howdy
puts "hi"
end
[1,2,3].howdy

-:4: private method `howdy' called for [1, 2, 3]:Array (NoMethodError)

m.
 
S

srdjan.m

Trans said:
Not Kernel, it becomes a private method of Object class.

def widget(tidbit)
tidbit
end

I was under the impression that the widget(tidbit) method becomes a
private method of Kernel not Object.

def hello
"hello"
end

class Object
def hello2
"hello2"
end
private_class_method :hello2
end

Object.private_methods.include?("hello") => true
Object.private_methods.include?("hello2") => true

Kernel.private_methods.include?("hello") => true
Kernel.private_methods.include?("hello2") => false

The code above demonstrates that hello is private to both Kernel and
Object as opposed to hello2 which is only private to Object.

Am I making a silly deduction mistake somewhere?

srdjan
 
S

Simen Edvardsen

def widget(tidbit)
tidbit
end

I was under the impression that the widget(tidbit) method becomes a
private method of Kernel not Object.

def hello
"hello"
end

class Object
def hello2
"hello2"
end
private_class_method :hello2
end

Object.private_methods.include?("hello") => true
Object.private_methods.include?("hello2") => true

Kernel.private_methods.include?("hello") => true
Kernel.private_methods.include?("hello2") => false

The code above demonstrates that hello is private to both Kernel and
Object as opposed to hello2 which is only private to Object.

Am I making a silly deduction mistake somewhere?

I thought it was added to Kernel too, but it appears not to be the case:

# test.rb
module Kernel
def self.method_added(m) puts "#{m} added to Kernel" end
end

class Object
def self.method_added(m) puts "#{m} added to Object" end
end

def x
end

$ ruby test.rb
x added to Object
 
S

srdjan.m

# test.rb
module Kernel
def self.method_added(m) puts "#{m} added to Kernel" end
end

class Object
def self.method_added(m) puts "#{m} added to Object" end
end

def x
end

$ ruby test.rb
x added to Object

- Simen

very good point, I guess in my post I made a mistake of not thinking
about "main" context.

srdjan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top