Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4279 > unrolled thread

Scope problem (?) in implementing Design Patterns in Ruby

Started byRichardOnRails <RichardDummyMailbox58407@USComputerGurus.com>
First post2011-05-11 10:15 -0700
Last post2011-05-13 12:29 -0500
Articles 20 on this page of 28 — 5 participants

Back to article view | Back to comp.lang.ruby


Contents

  Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-11 10:15 -0700
    Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-11 12:52 -0500
      Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-11 12:53 -0500
      Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-11 13:37 -0500
        Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-11 18:07 -0700
      Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-11 17:30 -0700
    Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 14:28 -0500
      Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 15:23 -0500
        Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-11 15:26 -0500
          Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 15:45 -0500
        Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 15:34 -0500
          Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-11 15:48 -0500
            Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-12 08:59 -0700
              Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-12 20:56 -0500
                Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 21:11 -0500
                  Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-12 21:18 -0500
                Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-13 00:28 -0700
                  Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-13 09:46 -0500
                    Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-13 10:55 -0700
                  Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-13 12:50 -0500
                    Re: Scope problem (?) in implementing Design Patterns in Ruby Steve Klabnik <steve@steveklabnik.com> - 2011-05-13 13:14 -0500
                    Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-23 19:24 -0700
                Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-24 20:33 -0700
                  Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-25 17:30 -0500
                    Re: Scope problem (?) in implementing Design Patterns in Ruby RichardOnRails <RichardDummyMailbox58407@USComputerGurus.com> - 2011-05-27 22:08 -0700
    Re: Scope problem (?) in implementing Design Patterns in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-12 21:20 -0500
      Re: Scope problem (?) in implementing Design Patterns in Ruby David Jacobs <developer@wit.io> - 2011-05-12 21:26 -0500
    Re: Scope problem (?) in implementing Design Patterns in Ruby Ryan Davis <ryand-ruby@zenspider.com> - 2011-05-13 12:29 -0500

Page 1 of 2  [1] 2  Next page →


#4279 — Scope problem (?) in implementing Design Patterns in Ruby

FromRichardOnRails <RichardDummyMailbox58407@USComputerGurus.com>
Date2011-05-11 10:15 -0700
SubjectScope problem (?) in implementing Design Patterns in Ruby
Message-ID<7e6155b0-8d33-4677-87ec-be2c3f0fca97@b7g2000prg.googlegroups.com>
Hi,

I'm hoping that employing "Design Patterns in Ruby" will lead to less
coding errors and more easily maintained code.  I'm stuck with the Not
pattern in the File Finding pattern in Chapter 15.

I've posted my code and output in http://www.pastie.org/1889586 and
http://www.pastie.org/188968, respectively.  Any idea about the cause
of the reported syntax error?

Thanks in Advance,
Richard

[toc] | [next] | [standalone]


#4281

FromDavid Jacobs <developer@wit.io>
Date2011-05-11 12:52 -0500
Message-ID<BANLkTi=U0vNmLikh+69DWKX=LLwKOGS2Eg@mail.gmail.com>
In reply to#4279
[Note:  parts of this message were removed to make it a legal post.]

I think the second link points to the wrong pastie.

I can see a couple of problems with the code, first of which is that you are
setting up Expression.new to require an argument but not passing it an
argument when you call All_basic.new, etc.

More than design patterns (which I think are a little much here), I would
suggest looking into Ruby's higher order functions like reduce and map. They
make your code a lot simpler. This is an alternate implementation I did. Let
me know what you think:

http://www.pastie.org/1889838

On Wed, May 11, 2011 at 1:20 PM, RichardOnRails <
RichardDummyMailbox58407@uscomputergurus.com> wrote:

> Hi,
>
> I'm hoping that employing "Design Patterns in Ruby" will lead to less
> coding errors and more easily maintained code.  I'm stuck with the Not
> pattern in the File Finding pattern in Chapter 15.
>
> I've posted my code and output in http://www.pastie.org/1889586 and
> http://www.pastie.org/188968, respectively.  Any idea about the cause
> of the reported syntax error?
>
> Thanks in Advance,
> Richard
>
>

[toc] | [prev] | [next] | [standalone]


#4282

FromDavid Jacobs <developer@wit.io>
Date2011-05-11 12:53 -0500
Message-ID<BANLkTimPEM-Xj4_=vn+6hg6h=nohtc5Bfg@mail.gmail.com>
In reply to#4281
[Note:  parts of this message were removed to make it a legal post.]

Correction: My bad, your version of All_basic doesn't inherit from
Expression like I thought it would from the pattern it follows.

On Wed, May 11, 2011 at 1:51 PM, David Jacobs <developer@wit.io> wrote:

> I think the second link points to the wrong pastie.
>
> I can see a couple of problems with the code, first of which is that you
> are setting up Expression.new to require an argument but not passing it an
> argument when you call All_basic.new, etc.
>
> More than design patterns (which I think are a little much here), I would
> suggest looking into Ruby's higher order functions like reduce and map. They
> make your code a lot simpler. This is an alternate implementation I did. Let
> me know what you think:
>
> http://www.pastie.org/1889838
>
> On Wed, May 11, 2011 at 1:20 PM, RichardOnRails <
> RichardDummyMailbox58407@uscomputergurus.com> wrote:
>
>> Hi,
>>
>> I'm hoping that employing "Design Patterns in Ruby" will lead to less
>> coding errors and more easily maintained code.  I'm stuck with the Not
>> pattern in the File Finding pattern in Chapter 15.
>>
>> I've posted my code and output in http://www.pastie.org/1889586 and
>> http://www.pastie.org/188968, respectively.  Any idea about the cause
>> of the reported syntax error?
>>
>> Thanks in Advance,
>> Richard
>>
>>
>

[toc] | [prev] | [next] | [standalone]


#4283

FromDavid Jacobs <developer@wit.io>
Date2011-05-11 13:37 -0500
Message-ID<BANLkTimYrbAg4fmj0MjHTeRcHRAy4Wo-Vw@mail.gmail.com>
In reply to#4281
[Note:  parts of this message were removed to make it a legal post.]

One more suggestion: if you're going to build a hierarchy, might as well
work the parent as much as possible. My final implementation would probably
look something like this:

http://www.pastie.org/1889969

On Wed, May 11, 2011 at 1:51 PM, David Jacobs <developer@wit.io> wrote:

> I think the second link points to the wrong pastie.
>
> I can see a couple of problems with the code, first of which is that you
> are setting up Expression.new to require an argument but not passing it an
> argument when you call All_basic.new, etc.
>
> More than design patterns (which I think are a little much here), I would
> suggest looking into Ruby's higher order functions like reduce and map. They
> make your code a lot simpler. This is an alternate implementation I did. Let
> me know what you think:
>
> http://www.pastie.org/1889838
>
> On Wed, May 11, 2011 at 1:20 PM, RichardOnRails <
> RichardDummyMailbox58407@uscomputergurus.com> wrote:
>
>> Hi,
>>
>> I'm hoping that employing "Design Patterns in Ruby" will lead to less
>> coding errors and more easily maintained code.  I'm stuck with the Not
>> pattern in the File Finding pattern in Chapter 15.
>>
>> I've posted my code and output in http://www.pastie.org/1889586 and
>> http://www.pastie.org/188968, respectively.  Any idea about the cause
>> of the reported syntax error?
>>
>> Thanks in Advance,
>> Richard
>>
>>
>

[toc] | [prev] | [next] | [standalone]


#4314

FromRichardOnRails <RichardDummyMailbox58407@USComputerGurus.com>
Date2011-05-11 18:07 -0700
Message-ID<96493b3e-1002-43fd-995c-4d3de445e350@l30g2000vbn.googlegroups.com>
In reply to#4283
On May 11, 2:37 pm, David Jacobs <develo...@wit.io> wrote:
> [Note:  parts of this message were removed to make it a legal post.]
>
> One more suggestion: if you're going to build a hierarchy, might as well
> work the parent as much as possible. My final implementation would probably
> look something like this:
>
> http://www.pastie.org/1889969
>
>
>
>
>
>
>
> On Wed, May 11, 2011 at 1:51 PM, David Jacobs <develo...@wit.io> wrote:
> > I think the second link points to the wrong pastie.
>
> > I can see a couple of problems with the code, first of which is that you
> > are setting up Expression.new to require an argument but not passing it an
> > argument when you call All_basic.new, etc.
>
> > More than design patterns (which I think are a little much here), I would
> > suggest looking into Ruby's higher order functions like reduce and map. They
> > make your code a lot simpler. This is an alternate implementation I did. Let
> > me know what you think:
>
> >http://www.pastie.org/1889838
>
> > On Wed, May 11, 2011 at 1:20 PM, RichardOnRails <
> > RichardDummyMailbox58...@uscomputergurus.com> wrote:
>
> >> Hi,
>
> >> I'm hoping that employing "Design Patterns in Ruby" will lead to less
> >> coding errors and more easily maintained code.  I'm stuck with the Not
> >> pattern in the File Finding pattern in Chapter 15.
>
> >> I've posted my code and output inhttp://www.pastie.org/1889586and
> >>http://www.pastie.org/188968, respectively.  Any idea about the cause
> >> of the reported syntax error?
>
> >> Thanks in Advance,
> >> Richard

Hi David,

Assuming I'm responding to the correct you version works perfectly, as
evidenced by results below gotten after I decorated your code with
puts versions.

The Design Patterns author,  Russ Olson, is aims to support a neat DSL
to support file search with arbitrary logical combinations of the
fundamental search elements ... which is my adopted goal.  So I've got
to master his approach to implementing this search language before I
can really weigh approaches.

7stud has responded with the key thing I saw but couldn't figure out
how to address the problem, so I've got to follow up on that now.

Thanks for very detailed responses in intrinsic Ruby education.

Best wishes,
Richard

======== Output ==========
>ruby DavesTempFilesDirs_2.rb

files.select &all
==========
AlternativeFileSearch.rb
DavesTempFilesDirs_1.rb
DavesTempFilesDirs_2.rb
FindFilesDirs-01.rb
FindFilesDirs-02.rb
FindFilesDirs.rb
TempFindFilesDirs.rb
TestDoc@.txt
TestFolder
TestFolder/TestDoc.txt

files.select &filename(/-01/))
==========
FindFilesDirs-01.rb

files.select &writable
==========
AlternativeFileSearch.rb
DavesTempFilesDirs_1.rb
DavesTempFilesDirs_2.rb
FindFilesDirs-01.rb
FindFilesDirs-02.rb
FindFilesDirs.rb
TempFindFilesDirs.rb
TestFolder/TestDoc.txt

files.select &_not(writable)
==========
TestDoc@.txt
TestFolder
>Exit code: 0

[toc] | [prev] | [next] | [standalone]


#4310

FromRichardOnRails <RichardDummyMailbox58407@USComputerGurus.com>
Date2011-05-11 17:30 -0700
Message-ID<e8c32bbc-90d6-4377-a1b8-70f0d2f46387@gu8g2000vbb.googlegroups.com>
In reply to#4281
On May 11, 1:52 pm, David Jacobs <develo...@wit.io> wrote:
> [Note:  parts of this message were removed to make it a legal post.]
>
> I think the second link points to the wrong pastie.
>
> I can see a couple of problems with the code, first of which is that you are
> setting up Expression.new to require an argument but not passing it an
> argument when you call All_basic.new, etc.
>
> More than design patterns (which I think are a little much here), I would
> suggest looking into Ruby's higher order functions like reduce and map. They
> make your code a lot simpler. This is an alternate implementation I did. Let
> me know what you think:
>
> http://www.pastie.org/1889838
>
> On Wed, May 11, 2011 at 1:20 PM, RichardOnRails <
>
>
>
>
>
>
>
> RichardDummyMailbox58...@uscomputergurus.com> wrote:
> > Hi,
>
> > I'm hoping that employing "Design Patterns in Ruby" will lead to less
> > coding errors and more easily maintained code.  I'm stuck with the Not
> > pattern in the File Finding pattern in Chapter 15.
>
> > I've posted my code and output inhttp://www.pastie.org/1889586and
> >http://www.pastie.org/188968, respectively.  Any idea about the cause
> > of the reported syntax error?
>
> > Thanks in Advance,
> > Richard

> I think the second link points to the wrong pastie.
It sure does.  I apologize for that error.  My results are at
http://www.pastie.org/1889681

> This is an alternate implementation I did. Let
> me know what you think:
>
> http://www.pastie.org/1889838

I downloaded your example, removed a few spurious "end" statement and
added a "p" to get the Regexp class name.
It ran perfectly as far as I can see,  but I'd have to format the
output to confirm that.
The code looks great,  but I'll await a real comparison until I get
the book's code working

> I can see a couple of problems with the code, first of which is that you are
> setting up Expression.new to require an argument but not passing it an
> argument when you call All_basic.new, etc.

Thanks for this possible solution.  I'll try to correct that after I
look at the other posts I've been lucky enough to get.

Best wishes,
Richard

[toc] | [prev] | [next] | [standalone]


#4286

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-11 14:28 -0500
Message-ID<2ac328808edcaa956da4f286617acefb@ruby-forum.com>
In reply to#4279
RichardOnRails wrote in post #998059:
>

1) Your case statement syntax doesn't work in ruby 1.9.2:

prog.rb:4: syntax error, unexpected ':', expecting keyword_then or ',' 
or ';' or '\n'

You have to use 'then' in place of a colon.

2) Next, I get this error:

prog.rb:67:in `evaluate': uninitialized constant Not::All (NameError)
  from prog.rb:74:in `<main>'

which relates to this code:

class Not < Expression
  def initialize(expression)
    @expression = expression
  end

  def evaluate(dir)
    all = All.new.evaluate(dir)
    other = @expression.evaluate(dir)
    all - other
  end
end

In ruby, constants are looked up like directories and files.  When you 
are inside the Not class (which is a module), the 'directory' you are in 
for constant lookups is the 'Not' directory.  When you write All.new, 
because the name All is not preceded by a directory name, ruby looks in 
the current 'directory' for the constant All.  The current directory is 
Not, so ruby is looking for Not::All, i.e. the 'file' All in the 
'directory' Not.  However, All is not defined inside Not, so you get an 
error.  In fact, there is not constant named All defined anywhere in 
your program.

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

[toc] | [prev] | [next] | [standalone]


#4292

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-11 15:23 -0500
Message-ID<4bfa1e5a3794daf70f5171094052dab0@ruby-forum.com>
In reply to#4286
7stud -- wrote in post #998081:
> RichardOnRails wrote in post #998059:
>>
>
> 1) Your case statement syntax doesn't work in ruby 1.9.2:
>
> prog.rb:4: syntax error, unexpected ':', expecting keyword_then or ','
> or ';' or '\n'
>
> You have to use 'then' in place of a colon.
>
> 2) Next, I get this error:
>
> prog.rb:67:in `evaluate': uninitialized constant Not::All (NameError)
>   from prog.rb:74:in `<main>'
>
> which relates to this code:
>
> class Not < Expression
>   def initialize(expression)
>     @expression = expression
>   end
>
>   def evaluate(dir)
>     all = All.new.evaluate(dir)
>     other = @expression.evaluate(dir)
>     all - other
>   end
> end
>
> In ruby, constants are looked up like directories and files (or if you
> prefer constants are 'lexically scoped').  When you
> are inside the Not class (which is a module), the 'directory' you are in
> for constant lookups is the 'Not' directory.  When you write All.new,
> because the name All is not preceded by a directory name, ruby looks in
> the current 'directory' for the constant All.  The current directory is
> Not, so ruby is looking for Not::All, i.e. the 'file' All in the
> 'directory' Not.  However, All is not defined inside Not, so you get an
> error.  In fact, there is no constant named All defined anywhere in
> your program, so the error is more serious than a scope problem.
>
> If you are inside a class/module and you need to access a class at the
> top level, you do this:
>
> class All
>   def greet
>     puts 'hi'
>   end
> end
>
>
> class Dog
>   def do_stuff
>     ::All.new.greet  #<*****
>   end
> end
>
> Dog.new.do_stuff  #=>hi
>

In fact, that is unnecessary:

class All
  def greet
    puts 'hi'
  end
end


class Dog
  def do_stuff
    All.new.greet  #<*****
  end
end

Dog.new.do_stuff  #=>hi


I guess the lookup actually starts at the toplevel.  So your error is a 
result of not defining the constant All at the toplevel, and ruby 
obfuscates the error by telling you that All is not defined inside the 
Not module, giving you the error message: Not::All doesn't exist.

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

[toc] | [prev] | [next] | [standalone]


#4293

FromDavid Jacobs <developer@wit.io>
Date2011-05-11 15:26 -0500
Message-ID<BANLkTi=qTAcJXkTeKSEeGDZeHs_Ma+pXUA@mail.gmail.com>
In reply to#4292
[Note:  parts of this message were removed to make it a legal post.]

I think the lookup starts at the innermost scope but since All isn't defined
in Not, it reaches the top-level.

On Wed, May 11, 2011 at 4:23 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

> 7stud -- wrote in post #998081:
> > RichardOnRails wrote in post #998059:
> >>
> >
> > 1) Your case statement syntax doesn't work in ruby 1.9.2:
> >
> > prog.rb:4: syntax error, unexpected ':', expecting keyword_then or ','
> > or ';' or '\n'
> >
> > You have to use 'then' in place of a colon.
> >
> > 2) Next, I get this error:
> >
> > prog.rb:67:in `evaluate': uninitialized constant Not::All (NameError)
> >   from prog.rb:74:in `<main>'
> >
> > which relates to this code:
> >
> > class Not < Expression
> >   def initialize(expression)
> >     @expression = expression
> >   end
> >
> >   def evaluate(dir)
> >     all = All.new.evaluate(dir)
> >     other = @expression.evaluate(dir)
> >     all - other
> >   end
> > end
> >
> > In ruby, constants are looked up like directories and files (or if you
> > prefer constants are 'lexically scoped').  When you
> > are inside the Not class (which is a module), the 'directory' you are in
> > for constant lookups is the 'Not' directory.  When you write All.new,
> > because the name All is not preceded by a directory name, ruby looks in
> > the current 'directory' for the constant All.  The current directory is
> > Not, so ruby is looking for Not::All, i.e. the 'file' All in the
> > 'directory' Not.  However, All is not defined inside Not, so you get an
> > error.  In fact, there is no constant named All defined anywhere in
> > your program, so the error is more serious than a scope problem.
> >
> > If you are inside a class/module and you need to access a class at the
> > top level, you do this:
> >
> > class All
> >   def greet
> >     puts 'hi'
> >   end
> > end
> >
> >
> > class Dog
> >   def do_stuff
> >     ::All.new.greet  #<*****
> >   end
> > end
> >
> > Dog.new.do_stuff  #=>hi
> >
>
> In fact, that is unnecessary:
>
> class All
>  def greet
>    puts 'hi'
>  end
> end
>
>
> class Dog
>  def do_stuff
>    All.new.greet  #<*****
>  end
> end
>
> Dog.new.do_stuff  #=>hi
>
>
> I guess the lookup actually starts at the toplevel.  So your error is a
> result of not defining the constant All at the toplevel, and ruby
> obfuscates the error by telling you that All is not defined inside the
> Not module, giving you the error message: Not::All doesn't exist.
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

[toc] | [prev] | [next] | [standalone]


#4297

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-11 15:45 -0500
Message-ID<f075a5f4506fda4a5fdf3adea6040bee@ruby-forum.com>
In reply to#4293
David Jacobs wrote in post #998093:
> I think the lookup starts at the innermost scope but since All isn't
> defined
> in Not, it reaches the top-level.

Yes, you are right:

1)
===
Constants defined within a class or module may be accessed unadorned 
anywhere within the class or module.

(Programming Ruby)
===

2)
===
Constants declared outside of a class or module are assigned global 
scope.

(http://www.techotopia.com/index.php/Ruby_Variable_Scope#Ruby_Constant_Scope)
===

So it's a case of the inner All hiding the global All.  And you can use 
the :: prefix to leap over an inner scope constant that hides a toplevel 
constant:

class All
  def greet
    puts 'All#greet'
  end
end

module C
  class All
     def greet
       puts "C::All#greet"
     end
  end

  class Dog
    def do_stuff
      ::All.new.greet  #<*****
    end
  end

end

C::Dog.new.do_stuff

--output:--
All#greet

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

[toc] | [prev] | [next] | [standalone]


#4295

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-11 15:34 -0500
Message-ID<9307dd9ab066746e74f5cbc1f8d02bbf@ruby-forum.com>
In reply to#4292
7stud -- wrote in post #998090:
>
> I guess the lookup actually starts at the toplevel.

Well, that's not true either.  If I define two All classes: one at the 
top level and one inside a module,

class All
  def greet
    puts 'All#greet'
  end
end

module C
  class All
     def greet
       puts "C::All#greet"
     end
  end

  class Dog
    def do_stuff
      All.new.greet  #<*****
    end
  end

end

C::Dog.new.do_stuff

--output:--
C::All#greet


Then if I delete C::All:

class All
  def greet
    puts 'All#greet'
  end
end

module C
  class Dog
    def do_stuff
      All.new.greet  #<*****
    end
  end
end

C::Dog.new.do_stuff

--output:--
All#greet


..which doesn't seem like that should work.

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

[toc] | [prev] | [next] | [standalone]


#4298

FromDavid Jacobs <developer@wit.io>
Date2011-05-11 15:48 -0500
Message-ID<BANLkTin3iMtQZ7MiAfhG0Eg=XwnrfgbwGA@mail.gmail.com>
In reply to#4295
[Note:  parts of this message were removed to make it a legal post.]

>
> ...which doesn't seem like that should work.


Why not?

[toc] | [prev] | [next] | [standalone]


#4383

FromRichardOnRails <RichardDummyMailbox58407@USComputerGurus.com>
Date2011-05-12 08:59 -0700
Message-ID<1a2fe490-3ad8-4c68-b1c2-2db31012a1a3@f31g2000pri.googlegroups.com>
In reply to#4298
Hi 7Zip and David,

Thanks very much to you both for hanging in there with me.

I hacked up a working version (agnostic for 1.8.6/1.9.2) here:
http://www.pastie.org/1893405,
with results here: http://www.pastie.org/1893420

I apologize for having screwed a Pastie URL last time.

I left comments in code for the hacks I made.  When I first
encountered the problem,  I made a feeble attempt at prefixing :: for
All but I failed to take note of the fact that the All definition was
subordinate to Expression.  That was stupid on my part, but I blame it
on the fact that I'm 77, have been retired from programming for 7
years and have only got serious about learning Ruby/Rails about a year
ago.  It's been a struggle; I used to be faster on the uptake.

BTW, David Black has a very nice exposition of lookup for methods and
classes on page 98 et seq of his "The Well-Grounded Rubyist", one of
my "bibles".

I don't know how my hacks will playout for the parsing scheme in
"Design Patterns in Ruby".  I going to follow it until this newsgroup
and/or I discover it's a fool's errand.

With my very  best wishes to you both and thanks for your generous
insights,
Richard

[toc] | [prev] | [next] | [standalone]


#4454

FromDavid Jacobs <developer@wit.io>
Date2011-05-12 20:56 -0500
Message-ID<C32E785734DD47739FFE9B46581FD36C@wit.io>
In reply to#4383
Hi Richard,

Happy to help.

I would definitely encourage you to not only look at design patterns (which are okay, but often overkill or already built in to Ruby) but also to Ruby's standard features to reduce code errors.

Ruby is a rich language. It has a lot of really well-crafted methods that take care of a most of the tedium of coding, and for that reason the standard library is well worth learning.

For example, in your code, instead of importing 'find' and using it to look at directories, use Ruby's built-in Dir module. Dir['**/*'] will get you all subdirectories and files recursively.

Second, look into higher order functions. They let you change code from this:

def evaluate(dir, arg) 
results = []
Dir['**/*'].each do |p|
next unless File.file? p
name = File.basename(p)
case arg
when String 
results << p if File.fnmatch(@arg, name)
when Regexp
results << p if @arg.match(name)
end
end
results
end

Into this:

def match?(str_or_re, file)
str_or_re.is_a?(String) ? File.fnmatch(str_or_re, file) : str_or_re =~ file
end

def evaluate(dir, arg)
Dir['**/*'].select {|f| File.file?(f) and match?(arg, f) }
end

Any time you see this pattern ...

my_temporary_variable = []

my_collection.each do |elem|
my_temporary_variable << elem if some_condition
end

return my_temporary_variable

.. you know that higher order functions would probably be a better solution. :)

Hope that helps, and good luck learning this beautiful language.

Cheers,
David


Any time you see this pattern in your code, you should automatically think
On Thursday, 12 May 2011 at 12:00 pm, RichardOnRails wrote: 
> Hi 7Zip and David,
> 
> Thanks very much to you both for hanging in there with me.
> 
> I hacked up a working version (agnostic for 1.8.6/1.9.2) here:
> http://www.pastie.org/1893405,
> with results here: http://www.pastie.org/1893420
> 
> I apologize for having screwed a Pastie URL last time.
> 
> I left comments in code for the hacks I made. When I first
> encountered the problem, I made a feeble attempt at prefixing :: for
> All but I failed to take note of the fact that the All definition was
> subordinate to Expression. That was stupid on my part, but I blame it
> on the fact that I'm 77, have been retired from programming for 7
> years and have only got serious about learning Ruby/Rails about a year
> ago. It's been a struggle; I used to be faster on the uptake.
> 
> BTW, David Black has a very nice exposition of lookup for methods and
> classes on page 98 et seq of his "The Well-Grounded Rubyist", one of
> my "bibles".
> 
> I don't know how my hacks will playout for the parsing scheme in
> "Design Patterns in Ruby". I going to follow it until this newsgroup
> and/or I discover it's a fool's errand.
> 
> With my very best wishes to you both and thanks for your generous
> insights,
> Richard
> 

[toc] | [prev] | [next] | [standalone]


#4456

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-12 21:11 -0500
Message-ID<955061948d14e1fc53cce28995debd08@ruby-forum.com>
In reply to#4454
David Jacobs wrote in post #998417:
> Hi Richard,
>
> Happy to help.
>
> I would definitely encourage you to not only look at design patterns
>

Hard to do when you are reading a book on Design Patterns, and you are 
trying to understand one of the examples.  I'm also currently reading 
the same book as RichardOnRails, but I'm not quite that far along.

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

[toc] | [prev] | [next] | [standalone]


#4457

FromDavid Jacobs <developer@wit.io>
Date2011-05-12 21:18 -0500
Message-ID<4FCD5733AB5D4C07A5E824A0F4402039@wit.io>
In reply to#4456
Hard to do when you are reading a book on Design Patterns, and you are 
> trying to understand one of the examples. I'm also currently reading 
> the same book as RichardOnRails, but I'm not quite that far along.
I've read the book. It's good but I think it's healthy to understand how design patterns aren't always necessary in Ruby like they are in Java. I'm just pointing out other, potentially more effective ways to write re-usable code.

Cheers,
David



[toc] | [prev] | [next] | [standalone]


#4465

FromRichardOnRails <RichardDummyMailbox58407@USComputerGurus.com>
Date2011-05-13 00:28 -0700
Message-ID<5e7178b8-0f8c-4039-b43c-be0c350c0b58@dn9g2000vbb.googlegroups.com>
In reply to#4454
On May 12, 9:56 pm, David Jacobs <develo...@wit.io> wrote:
> Hi Richard,
>
> Happy to help.
>
> I would definitely encourage you to not only look at design patterns (which are okay, but often overkill or already built in to Ruby) but also to Ruby's standard features to reduce code errors.
>
> Ruby is a rich language. It has a lot of really well-crafted methods that take care of a most of the tedium of coding, and for that reason the standard library is well worth learning.
>
> For example, in your code, instead of importing 'find' and using it to look at directories, use Ruby's built-in Dir module. Dir['**/*'] will get you all subdirectories and files recursively.
>
> Second, look into higher order functions. They let you change code from this:
>
> def evaluate(dir, arg)
> results = []
> Dir['**/*'].each do |p|
> next unless File.file? p
> name = File.basename(p)
> case arg
> when String
> results << p if File.fnmatch(@arg, name)
> when Regexp
> results << p if @arg.match(name)
> end
> end
> results
> end
>
> Into this:
>
> def match?(str_or_re, file)
> str_or_re.is_a?(String) ? File.fnmatch(str_or_re, file) : str_or_re =~ file
> end
>
> def evaluate(dir, arg)
> Dir['**/*'].select {|f| File.file?(f) and match?(arg, f) }
> end
>
> Any time you see this pattern ...
>
> my_temporary_variable = []
>
> my_collection.each do |elem|
> my_temporary_variable << elem if some_condition
> end
>
> return my_temporary_variable
>
> .. you know that higher order functions would probably be a better solution. :)
>
> Hope that helps, and good luck learning this beautiful language.
>
> Cheers,
> David
>
> Any time you see this pattern in your code, you should automatically think
>
>
>
>
>
>
>
> On Thursday, 12 May 2011 at 12:00 pm, RichardOnRails wrote:
> > Hi 7Zip and David,
>
> > Thanks very much to you both for hanging in there with me.
>
> > I hacked up a working version (agnostic for 1.8.6/1.9.2) here:
> >http://www.pastie.org/1893405,
> > with results here:http://www.pastie.org/1893420
>
> > I apologize for having screwed a Pastie URL last time.
>
> > I left comments in code for the hacks I made. When I first
> > encountered the problem, I made a feeble attempt at prefixing :: for
> > All but I failed to take note of the fact that the All definition was
> > subordinate to Expression. That was stupid on my part, but I blame it
> > on the fact that I'm 77, have been retired from programming for 7
> > years and have only got serious about learning Ruby/Rails about a year
> > ago. It's been a struggle; I used to be faster on the uptake.
>
> > BTW, David Black has a very nice exposition of lookup for methods and
> > classes on page 98 et seq of his "The Well-Grounded Rubyist", one of
> > my "bibles".
>
> > I don't know how my hacks will playout for the parsing scheme in
> > "Design Patterns in Ruby". I going to follow it until this newsgroup
> > and/or I discover it's a fool's errand.
>
> > With my very best wishes to you both and thanks for your generous
> > insights,
> > Richard

Hi Dave,

I checked out your Version 2.  Added puts stuff and restriction to
names of files, which should probably be another one of your methods
instead of my hard-coding.
Expanded code: http://www.pastie.org/1895917
Output: http://www.pastie.org/1895932

I have only one question: I put a space between the & and the method
following it; the code broke.  Is the construct you used documented on-
line somewhere?  I've peeked at lambda documentation so I've got the
drift but I haven't used it in any of my code so far.

I copied your memo on the approach and plan to review it and use/
expand your code after I finish getting Russ Olson's code working or
giving up on it.  To his credit,  he posted his code on-line so I can
avoid typos and he included an apparently thorough set of unit tests.
So I've got a lot of studying to do.

Again,  thanks for your guidance and great ideas,
Richard

[toc] | [prev] | [next] | [standalone]


#4496

FromDavid Jacobs <developer@wit.io>
Date2011-05-13 09:46 -0500
Message-ID<BANLkTikSRQ=0d0TRYhYJoheErL0+E9hG3g@mail.gmail.com>
In reply to#4465
[Note:  parts of this message were removed to make it a legal post.]

Hi Richard,

The & has to be adjacent to "all" or "filename" to act as expected. The &
can mean one of two related things when you pass it to a method:

1. It can mean "pass the lambda expression that this variable points to into
a block". That makes the following to snippets equivalent:

# Snippet 1
filter_function = lambda {|x| x == 2 }
[1, 2, 3, 4, 2].select &filter_function

# Snippet 2
[1, 2, 3, 4, 2].select {|x| x == 2 }

2. It can mean "pass the instance method named by a symbol, and treat it as
a block".

That makes these two equivalent:

# Snippet 1
[1, 2, 3, 4, 2].select {|x| x.even? }

# Snippet 2
[1, 2, 3, 4, 2].select(&:even?)

So what my code is doing is calling the all method, for example, and that
method's sole job is to return a lambda that can then be passed in per the
above. You don't have to wrap the lambda in a method like I did. You could
also just say:

all = lambda {|x| x }

I hope that makes things a little clearer!

Cheers,
David
On Fri, May 13, 2011 at 3:31 AM, RichardOnRails <
RichardDummyMailbox58407@uscomputergurus.com> wrote:

> Hi Dave,
>
> I checked out your Version 2.  Added puts stuff and restriction to
> names of files, which should probably be another one of your methods
> instead of my hard-coding.
> Expanded code: http://www.pastie.org/1895917
> Output: http://www.pastie.org/1895932
>
> I have only one question: I put a space between the & and the method
> following it; the code broke.  Is the construct you used documented on-
> line somewhere?  I've peeked at lambda documentation so I've got the
> drift but I haven't used it in any of my code so far.
>
> I copied your memo on the approach and plan to review it and use/
> expand your code after I finish getting Russ Olson's code working or
> giving up on it.  To his credit,  he posted his code on-line so I can
> avoid typos and he included an apparently thorough set of unit tests.
> So I've got a lot of studying to do.
>
> Again,  thanks for your guidance and great ideas,
> Richard
>
>

[toc] | [prev] | [next] | [standalone]


#4505

FromRichardOnRails <RichardDummyMailbox58407@USComputerGurus.com>
Date2011-05-13 10:55 -0700
Message-ID<348b889d-b81c-430e-aabc-ad20e5a462a3@s9g2000yqm.googlegroups.com>
In reply to#4496
On May 13, 10:46 am, David Jacobs <develo...@wit.io> wrote:
> [Note:  parts of this message were removed to make it a legal post.]
>
> Hi Richard,
>
> The & has to be adjacent to "all" or "filename" to act as expected. The &
> can mean one of two related things when you pass it to a method:
>
> 1. It can mean "pass the lambda expression that this variable points to into
> a block". That makes the following to snippets equivalent:
>
> # Snippet 1
> filter_function = lambda {|x| x == 2 }
> [1, 2, 3, 4, 2].select &filter_function
>
> # Snippet 2
> [1, 2, 3, 4, 2].select {|x| x == 2 }
>
> 2. It can mean "pass the instance method named by a symbol, and treat it as
> a block".
>
> That makes these two equivalent:
>
> # Snippet 1
> [1, 2, 3, 4, 2].select {|x| x.even? }
>
> # Snippet 2
> [1, 2, 3, 4, 2].select(&:even?)
>
> So what my code is doing is calling the all method, for example, and that
> method's sole job is to return a lambda that can then be passed in per the
> above. You don't have to wrap the lambda in a method like I did. You could
> also just say:
>
> all = lambda {|x| x }
>
> I hope that makes things a little clearer!
>
> Cheers,
> David
> On Fri, May 13, 2011 at 3:31 AM, RichardOnRails <
>
>
>
>
>
>
>
> RichardDummyMailbox58...@uscomputergurus.com> wrote:
> > Hi Dave,
>
> > I checked out your Version 2.  Added puts stuff and restriction to
> > names of files, which should probably be another one of your methods
> > instead of my hard-coding.
> > Expanded code:http://www.pastie.org/1895917
> > Output:http://www.pastie.org/1895932
>
> > I have only one question: I put a space between the & and the method
> > following it; the code broke.  Is the construct you used documented on-
> > line somewhere?  I've peeked at lambda documentation so I've got the
> > drift but I haven't used it in any of my code so far.
>
> > I copied your memo on the approach and plan to review it and use/
> > expand your code after I finish getting Russ Olson's code working or
> > giving up on it.  To his credit,  he posted his code on-line so I can
> > avoid typos and he included an apparently thorough set of unit tests.
> > So I've got a lot of studying to do.
>
> > Again,  thanks for your guidance and great ideas,
> > Richard

Hi again, Dave,

> I hope that makes things a little clearer!

Crystal! as Kaffee said to Col. Jessep during their Gitmo meeting (A
Few Good Men)

Thanks for illuminating another area of Ruby where my proficiency
needs enhancement :-)

Best,
Richard

[toc] | [prev] | [next] | [standalone]


#4504

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-13 12:50 -0500
Message-ID<4bc0d1972260442c6d44caba7e6eefaf@ruby-forum.com>
In reply to#4465
RichardOnRails wrote in post #998460:
>
> I have only one question: I put a space between the & and the method
> following it; the code broke.  Is the construct you used documented on-
> line somewhere?  I've peeked at lambda documentation so I've got the
> drift but I haven't used it in any of my code so far.
>

Your bible, "The Well-Grounded Rubyist" uses the &:meth_name construct a 
lot, and David Black says that the '&' in '&:meth_name' does two things:

1) It calls the specified object's to_proc() method.  And Symbol#to_proc 
is defined like this:

class Symbol
  def to_proc
    proc{|obj, *args| obj.send(self, *args)}
    #self=:meth_name
  end
end

That just creates a function that takes an object as an argument and 
sends() the :meth_name to the object, i.e. the function calls the 
:meth_name method on the object.

2) The '&' tells ruby to convert the Proc to a block.

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

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.ruby


csiph-web