Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > classless methods

Reply
Thread Tools

classless methods

 
 
Dave Rose
Guest
Posts: n/a
 
      10-27-2006
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?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Daniel Berger
Guest
Posts: n/a
 
      10-27-2006
Dave Rose wrote:
> what class does a classless independent method belong too?


<snip>

Kernel.

Regards,

Dan

 
Reply With Quote
 
 
 
 
Trans
Guest
Posts: n/a
 
      10-27-2006

Dave Rose wrote:
> 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.

 
Reply With Quote
 
Eero Saynatkari
Guest
Posts: n/a
 
      10-27-2006
--5vb8QKeSi34BFwx0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On 2006.10.28 01:10, Dave Rose wrote:
> 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)

iD8DBQFFQj1Y7Nh7RM4TrhIRAuRxAKDCdjQYRNvXDEYX1Mj9VG 9bonUGPgCgocMF
Prg9lfPt0xaky9ryqSn+rkQ=
=zLWP
-----END PGP SIGNATURE-----

--5vb8QKeSi34BFwx0--

 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      10-27-2006

Daniel Berger wrote:
> Dave Rose wrote:
> > what class does a classless independent method belong too?

>
> <snip>
>
> Kernel.


Whoops. Make that Object.

- Dan

 
Reply With Quote
 
matt neuburg
Guest
Posts: n/a
 
      10-27-2006
Dave Rose <(E-Mail Removed)> wrote:

> 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?


<http://www.rubycentral.com/faq/rubyfaq-7.html#ss7.4>

m.


--
matt neuburg, phd = http://www.velocityreviews.com/forums/(E-Mail Removed), http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
 
Reply With Quote
 
Jason Merrill
Guest
Posts: n/a
 
      10-27-2006
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>


On 10/27/06, matt neuburg <(E-Mail Removed)> wrote:
> Dave Rose <(E-Mail Removed)> wrote:
>
> > 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?

>
> <http://www.rubycentral.com/faq/rubyfaq-7.html#ss7.4>
>
> m.
>
>
> --
> matt neuburg, phd = (E-Mail Removed), http://www.tidbits.com/matt/
> Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
> AppleScript - http://www.amazon.com/gp/product/0596102119
> Read TidBITS! It's free and smart. http://www.tidbits.com
>
>


 
Reply With Quote
 
matt neuburg
Guest
Posts: n/a
 
      10-27-2006
Jason Merrill <(E-Mail Removed)> wrote:

> 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.


--
matt neuburg, phd = (E-Mail Removed), http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
 
Reply With Quote
 
Hugh Sasse
Guest
Posts: n/a
 
      10-27-2006
On Sat, 28 Oct 2006, matt neuburg wrote:

> Jason Merrill <(E-Mail Removed)> wrote:
>
> > 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

 
Reply With Quote
 
matt neuburg
Guest
Posts: n/a
 
      10-27-2006
Hugh Sasse <(E-Mail Removed)> wrote:

> On Sat, 28 Oct 2006, matt neuburg wrote:
>
> > Jason Merrill <(E-Mail Removed)> wrote:
> >
> > > 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.



--
matt neuburg, phd = (E-Mail Removed), http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IP CLASSLESS not working Max Cisco 13 05-09-2008 09:03 AM
ip classless and auto-summary jh3ang Cisco 3 08-05-2006 03:10 PM
Question about Redistributing Between Classful and Classless Protocols ws00sw@gmail.com Cisco 0 06-28-2005 12:51 PM
Is classless worth consideration David MacQuigg Python 33 05-03-2004 05:17 PM
[Classless] Just to be sure... Yermat Python 0 04-30-2004 02:11 PM



Advertisments