ruby

so i decided to learn ruby and just wanted to make sure im learning correctly.
using (in ruby) module.method(variable) is this equivalent to namespace::function(arguements)?
closed account (1yR4jE8b)
This is a C++ forum, you'd get better answers on a Ruby form. Then again, I am a ruby developer by trade so I can answer this.

The short answer is "sortof". The long answer is that modules serve a very different purposes than namespaces, but can be used in that purpose if you desire. Modules are typically used for Mixins, which allow you to dynamically add methods to objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
module Foo
  def say_hello
    puts "hello world"
  end
end

class Bar
end

x = Bar.new
puts x.respond_to? :say_hello

class Bar
  include Foo
end

puts x.respond_to? :say_hello
x.say_hello
closed account (1yR4jE8b)
If you want to use a module like a namespace, the syntax is a bit different, you must prefix the method name with either the ModuleName:: or self::

1
2
3
4
5
6
7
8
module Foo
  def self::say_hello
    puts "hello world"
  end
end

Foo::say_hello

@darkestfright,

It's the lounge. Lounge is for any topic.
closed account (1yR4jE8b)
I know, noticed how I answered him anyway? This is entry-level Ruby stuff though, and is answered in the documentation/wiki/website/beginner ruby forums. I was trying to direct him to a place that might be better suited to helping him learn.
Topic archived. No new replies allowed.