Newbie questions on the meanings of things in Ruby

Hi,

I am tasked with the development of puppet provider. I am a few days
into Ruby and looking at the existing providers I am finding it hard to
interpret - I went over Ruby pargprog book and I am still not sure what
the following syntax means. I was hoping someone might be kind enough to
guide me through the terms here. This is the code fragment I am
struggling to understand:

Puppet::Type.type(:file).provide :posix do
  desc "Uses POSIX functionality to manage file ownership and
permissions."

  confine :feature => :posix

  include Puppet::Util::POSIX
  include Puppet::Util::Warnings

  require 'etc'

  def uid2name(id)
    return id.to_s if id.is_a?(Symbol) or id.is_a?(String)
    return nil if id > Puppet[:maximum_uid].to_i

    begin
      user = Etc.getpwuid(id)
    rescue TypeError, ArgumentError
      return nil
    end

    if user.uid == ""
      return nil
    else
      return user.name
    end
  end

....

end

In particular I am finding it hard to understand what the first line
means -

Puppet::Type.type(:file).provide :posix do

?

Is this some sort of dynamic class (Type) extension? If so is there a
resource I can go to dig up a bit more on this?

Thanks,
Alex.

···

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

I've never used Puppet, so I can only comment on the syntax part:

Puppet::Type is a constant, which looks like it refers to a class or module.
You are calling the method #type of that class or module, passing the
parameter :file (which is a symbol).
In order to read more documentation about this method, you can ri
Puppet::Type#type or look into Puppet's documentation.
This method returns an object, on which you call the method #provide,
passing the argument :posix and a block. A block is what is contained
between the do...end syntax, so basically the rest of the snippet is
the block you pass.

Without knowing anything about Puppet, it seems that this piece of
code creates a new class or an extension for Puppet's file type, in
order to provide this uid2name method.

Hope this helps,

Jesus.

···

On Mon, Jul 16, 2012 at 9:04 AM, Alex S. <lists@ruby-forum.com> wrote:

Hi,

I am tasked with the development of puppet provider. I am a few days
into Ruby and looking at the existing providers I am finding it hard to
interpret - I went over Ruby pargprog book and I am still not sure what
the following syntax means. I was hoping someone might be kind enough to
guide me through the terms here. This is the code fragment I am
struggling to understand:

Puppet::Type.type(:file).provide :posix do
  desc "Uses POSIX functionality to manage file ownership and
permissions."

  confine :feature => :posix

  include Puppet::Util::POSIX
  include Puppet::Util::Warnings

  require 'etc'

  def uid2name(id)
    return id.to_s if id.is_a?(Symbol) or id.is_a?(String)
    return nil if id > Puppet[:maximum_uid].to_i

    begin
      user = Etc.getpwuid(id)
    rescue TypeError, ArgumentError
      return nil
    end

    if user.uid == ""
      return nil
    else
      return user.name
    end
  end

....

end

In particular I am finding it hard to understand what the first line
means -

Puppet::Type.type(:file).provide :posix do

?

Is this some sort of dynamic class (Type) extension? If so is there a
resource I can go to dig up a bit more on this?

I'm going to explain the sentence syntactically, not what it does:

Puppet::Type.type(:file).provide :posix do

Puppet is a module which have a class Type,
calls method type with the parameter :file(which is a symbol),
then call a method provide(the Puppet lib uses method chaining to create a
domain specific language(DSL), google the terms if you must),
and this method provide has a parameter :posix.

The do .. end is a block.

I advise you to get the basics straight before you dive into that kind of
code.

Have a good day!

···

On Mon, Jul 16, 2012 at 4:14 AM, Jesús Gabriel y Galán < jgabrielygalan@gmail.com> wrote:

On Mon, Jul 16, 2012 at 9:04 AM, Alex S. <lists@ruby-forum.com> wrote:
> Hi,
>
> I am tasked with the development of puppet provider. I am a few days
> into Ruby and looking at the existing providers I am finding it hard to
> interpret - I went over Ruby pargprog book and I am still not sure what
> the following syntax means. I was hoping someone might be kind enough to
> guide me through the terms here. This is the code fragment I am
> struggling to understand:
>
> Puppet::Type.type(:file).provide :posix do
> desc "Uses POSIX functionality to manage file ownership and
> permissions."
>
> confine :feature => :posix
>
> include Puppet::Util::POSIX
> include Puppet::Util::Warnings
>
> require 'etc'
>
> def uid2name(id)
> return id.to_s if id.is_a?(Symbol) or id.is_a?(String)
> return nil if id > Puppet[:maximum_uid].to_i
>
> begin
> user = Etc.getpwuid(id)
> rescue TypeError, ArgumentError
> return nil
> end
>
> if user.uid == ""
> return nil
> else
> return user.name
> end
> end
>
> ....
>
> end
>
> In particular I am finding it hard to understand what the first line
> means -
>
> Puppet::Type.type(:file).provide :posix do
>
> ?
>
> Is this some sort of dynamic class (Type) extension? If so is there a
> resource I can go to dig up a bit more on this?

I've never used Puppet, so I can only comment on the syntax part:

Puppet::Type is a constant, which looks like it refers to a class or
module.
You are calling the method #type of that class or module, passing the
parameter :file (which is a symbol).
In order to read more documentation about this method, you can ri
Puppet::Type#type or look into Puppet's documentation.
This method returns an object, on which you call the method #provide,
passing the argument :posix and a block. A block is what is contained
between the do...end syntax, so basically the rest of the snippet is
the block you pass.

Without knowing anything about Puppet, it seems that this piece of
code creates a new class or an extension for Puppet's file type, in
order to provide this uid2name method.

Hope this helps,

Jesus.