Classless methods

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

Dave Rose wrote:

what class does a classless independent method belong too?

<snip>

Kernel.

Regards,

Dan

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.

Ruby can tell you.

  def widget
    # ...
  end

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

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

···

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:

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&gt;

m.

···

Dave Rose <bitdoger2@yahoo.com> 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?

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
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

Actually, it looks like it becomes a public instance method of Object:

irb(main):001:0> def hello; "hi"; end
=> nil
irb(main):002:0> self.class
=> Object
irb(main):004:0> self.public_methods.include?("hello")
=> true
irb(main):005:0> Object.private_methods.include?("hello")
=> false
irb(main):006:0> Object.private_instance_methods.include?("hello")
=> false
irb(main):007:0> Object.public_instance_methods.include?("hello")
=> true
irb(main):008:0> (class << self; self;
end).public_instance_methods.include?("hello")
=> true

/Nick

···

On 10/27/06, Trans <transfire@gmail.com> wrote:

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

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

T.

Daniel Berger wrote:

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

<snip>

Kernel.

Whoops. Make that Object.

- Dan

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 <matt@tidbits.com> wrote:

Dave Rose <bitdoger2@yahoo.com> 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&gt;

m.

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
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

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

···

Jason Merrill <jason.merrill@yale.edu> 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"

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
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

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

        Hugh

···

On Sat, 28 Oct 2006, matt neuburg wrote:

Jason Merrill <jason.merrill@yale.edu> 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-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.

···

Hugh Sasse <hgs@dmu.ac.uk> wrote:

On Sat, 28 Oct 2006, matt neuburg wrote:

> Jason Merrill <jason.merrill@yale.edu> 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. :slight_smile:

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
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

Trans wrote:

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

>

        [...]

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

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)

Thank you. Saw the other post about that being a private method after
posting. I'd completely forgotten about that (as you can tell).

m.

        Thanks
        Hugh

···

On Sat, 28 Oct 2006, matt neuburg wrote:

Hugh Sasse <hgs@dmu.ac.uk> wrote:
> On Sat, 28 Oct 2006, matt neuburg wrote:

Trans wrote:
> 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?

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

···

On 10/28/06, srdjan.m <srdjan.marinovic@gmail.com> wrote:

srdjan

--
- Simen

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

srdjan

···

On Oct 28, 12:48 am, "Simen Edvardsen" <toal...@gmail.com> wrote:

# 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

> srdjan--
- Simen