From: Thiago Massa Date: 2012-07-16T22:09:31+09:00 Subject: Re: Newbie questions on the meanings of things in Ruby --90e6ba61358adc0ac704c4f2239f Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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=FAs Gabriel y Gal=E1n < jgabrielygalan@gmail.com> wrote: > On Mon, Jul 16, 2012 at 9:04 AM, Alex S. 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 t= o > > 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 =3D> :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 =3D Etc.getpwuid(id) > > rescue TypeError, ArgumentError > > return nil > > end > > > > if user.uid =3D=3D "" > > 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. > > --90e6ba61358adc0ac704c4f2239f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I'm going to explain the sentence syntactically, not what it does:
=
Puppet::Type.type(:fi= le).provide :posix do<= /div>

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

The do .. end is a bl= ock.
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=FA= s Gabriel y Gal=E1n <jgabrielygalan@gmail.com> wrote:=
On Mon, Jul 16, 2012 at 9:04 AM, Al= ex S. <lists@ruby-forum.com&= gt; 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 t= o
> interpret - I went over Ruby pargprog book and I am still not sure wha= t
> the following syntax means. I was hoping someone might be kind enough = to
> guide me through the terms here. This is the code =A0fragment I am
> struggling to understand:
>
> Puppet::Type.type(:file).provide :posix do
> =A0 desc "Uses POSIX functionality to manage file ownership and > permissions."
>
> =A0 confine :feature =3D> :posix
>
> =A0 include Puppet::Util::POSIX
> =A0 include Puppet::Util::Warnings
>
> =A0 require 'etc'
>
> =A0 def uid2name(id)
> =A0 =A0 return id.to_s if id.is_a?(Symbol) or id.is_a?(String)
> =A0 =A0 return nil if id > Puppet[:maximum_uid].to_i
>
> =A0 =A0 begin
> =A0 =A0 =A0 user =3D Etc.getpwuid(id)
> =A0 =A0 rescue TypeError, ArgumentError
> =A0 =A0 =A0 return nil
> =A0 =A0 end
>
> =A0 =A0 if user.uid =3D=3D ""
> =A0 =A0 =A0 return nil
> =A0 =A0 else
> =A0 =A0 =A0 return user= .name
> =A0 =A0 end
> =A0 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<= br> > 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.


--90e6ba61358adc0ac704c4f2239f--