[#366855] what is the correct way to extend native methods? — Maurizio De Santis <desantis.maurizio@...>

Hello!

15 messages 2010/08/01
[#366857] Re: what is the correct way to extend native methods? — James Harrison <oscartheduck@...> 2010/08/01

> return self.select{ |val| val.to_s =~ args[0] } if args.size == 1

[#366864] Re: what is the correct way to extend native methods? — Maurizio De Santis <desantis.maurizio@...> 2010/08/01

James Harrison wrote:

[#366866] Re: what is the correct way to extend native methods? — Brian Candler <b.candler@...> 2010/08/01

Maurizio De Santis wrote:

[#366916] How to remove leading &nbsp; from string — Lucky Nl <lakshmi27.u@...>

Hi

13 messages 2010/08/02

[#366931] Iteration through File.file? misses entries for which File.file?(entry) == true — Kyle Barbour <kyle@...>

Hello everyone,

11 messages 2010/08/02

[#367167] Project name ownership and conflict — Emmanuel Gomez <emmanuel.gomez@...>

I recently had a discussion with a fellow Ruby developer that revealed a

13 messages 2010/08/05

[#367169] Abstracting exception handling — Martin Hansen <mail@...>

Hello,

15 messages 2010/08/05
[#367173] Re: Abstracting exception handling — Brian Candler <b.candler@...> 2010/08/05

> I suspect something is going out of scope and lost?

[#367176] Re: Abstracting exception handling — Martin Hansen <mail@...> 2010/08/05

I was hoping for a setup like this in two files:

[#367177] Re: Abstracting exception handling — Andrew Wagner <wagner.andrew@...> 2010/08/05

What kind of stuff are you doing in my_script.rb? Defining a class? A

[#367179] Re: Abstracting exception handling — Martin Hansen <mail@...> 2010/08/05

Andrew Wagner wrote:

[#367372] Is there any human talkable ruby library? — Sniper Abandon <sathish.salem.1984@...>

Is there any human talk able(like Eliza ) ruby library?

12 messages 2010/08/09

[#367438] Determining whether the running ruby is outdated? — Lars Olsson <lasso@...>

Hi,

14 messages 2010/08/10

[#367540] Ruby is in Grave Danger! — Gregory Brown <gregory.t.brown@...>

Dear Friends,

34 messages 2010/08/11

[#367631] Parsing, BNF, TreeTop, GhostWheel, ... — Philipp Kempgen <lists@...>

Hi,

13 messages 2010/08/12

[#367664] libstdc++ — Pw Ktp <amar.seeam@...>

when trying to install a gem i am getting a 'libstdc++' not installed

24 messages 2010/08/13
[#367666] Re: libstdc++ — Brian Candler <b.candler@...> 2010/08/13

Pw Ktp wrote:

[#367668] Re: libstdc++ — Pw Ktp <amar.seeam@...> 2010/08/13

Brian Candler wrote:

[#367670] Re: libstdc++ — Brian Candler <b.candler@...> 2010/08/13

OK, probably missing headers as Daniel said. Try:

[#367671] Re: libstdc++ — Pw Ktp <amar.seeam@...> 2010/08/13

Brian Candler wrote:

[#367703] Question about learning Ruby effectively — Chan Nguyen <cnguyen@...>

Hi everyone,

12 messages 2010/08/14

[#367781] Unix Philosophy in Ruby Programing — Diego Bernardes <di3go.bernardes@...>

I use Linux about 5 years, but, this year that i started to "use" linux.

22 messages 2010/08/16

[#367833] can i do this in ruby? a simulation process — Bruce Wayner <winshocker@...>

still i don't know how to begin my program on this problem:

56 messages 2010/08/17
[#367834] Re: can i do this in ruby? a simulation process — Jean-Julien Fleck <jeanjulien.fleck@...> 2010/08/17

> Requirements:

[#367837] Re: can i do this in ruby? a simulation process — Bruce Wayner <winshocker@...> 2010/08/17

Jean-Julien Fleck wrote:

[#367839] Re: can i do this in ruby? a simulation process — Jean-Julien Fleck <jeanjulien.fleck@...> 2010/08/17

> Cheers Thanks, anyway i already did writing and other stuff but the only

[#367849] Re: can i do this in ruby? a simulation process — Andrew Wagner <wagner.andrew@...> 2010/08/17

Hmm, I may or may not disagree with you on what the output should be. I

[#367850] Re: can i do this in ruby? a simulation process — Brian Candler <b.candler@...> 2010/08/17

Andrew Wagner wrote:

[#367853] Re: can i do this in ruby? a simulation process — Andrew Wagner <wagner.andrew@...> 2010/08/17

>

[#367858] Re: can i do this in ruby? a simulation process — Bruce Wayner <winshocker@...> 2010/08/17

A superhighway connects one large metropolitan area to another.

[#367862] Re: can i do this in ruby? a simulation process — Andrew Wagner <wagner.andrew@...> 2010/08/17

>

[#367867] Re: can i do this in ruby? a simulation process — Jean-Julien Fleck <jeanjulien.fleck@...> 2010/08/17

> You bring up an interesting point about going

[#367873] Re: can i do this in ruby? a simulation process — Andrew Wagner <wagner.andrew@...> 2010/08/17

Well, it may be interesting, but not in terms of the question originally

[#367911] Re: can i do this in ruby? a simulation process — brabuhr@... 2010/08/17

On Tue, Aug 17, 2010 at 7:34 AM, Andrew Wagner <wagner.andrew@gmail.com> wrote:

[#367922] Re: can i do this in ruby? a simulation process — brabuhr@... 2010/08/17

On Tue, Aug 17, 2010 at 1:19 PM, <brabuhr@gmail.com> wrote:

[#367937] Re: can i do this in ruby? a simulation process — Bruce Wayner <winshocker@...> 2010/08/18

can someone post some code here: I'm only a newbie in ruby :( and

[#367946] Re: can i do this in ruby? a simulation process — Brian Candler <b.candler@...> 2010/08/18

Bruce Wayner wrote:

[#367952] Re: can i do this in ruby? a simulation process — Bruce Wayner <winshocker@...> 2010/08/18

sorry I'm totally suck in ruby here is my code:

[#367965] Re: can i do this in ruby? a simulation process — Brian Candler <b.candler@...> 2010/08/18

> sorry I'm totally suck in ruby here is my code:

[#367967] Re: can i do this in ruby? a simulation process — Brian Candler <b.candler@...> 2010/08/18

>> if $delay1==0;

[#367884] Making File.open work on gzipped files — Martin Hansen <mail@...>

Hello all,

15 messages 2010/08/17
[#367893] Re: Making File.open work on gzipped files — Brian Candler <b.candler@...> 2010/08/17

> This works nicely, but I would like it to work on gzipped files too.

[#367919] Re: Making File.open work on gzipped files — Martin Hansen <mail@...> 2010/08/17

Thanks Brian,

[#367910] Ruby GC question (MRI, JRuby, etc) — Chuck Remes <cremes.devlist@...>

My basic understanding of the garbage collectors in use by the various Ruby runtimes is that they all search for objects from a "root" memory object. If an object cannot be reached from this root, then it is collected.

11 messages 2010/08/17

[#367983] Ruby 1.9.2 is released — "Yuki Sonoda (Yugui)" <yugui@...>

-----BEGIN PGP SIGNED MESSAGE-----

23 messages 2010/08/18
[#368021] Re: [ANN] Ruby 1.9.2 is released — botp <botpena@...> 2010/08/19

2010/8/18 Yuki Sonoda (Yugui) <yugui@yugui.jp>:

[#368023] Re: [ANN] Ruby 1.9.2 is released — botp <botpena@...> 2010/08/19

> weird, since rvm does install it fine.

[#368000] Ruby Code Parsing — Jonathan Bale <webmaster@...>

I have a Perl friend asking me questions about how ruby parses its code.

15 messages 2010/08/18

[#368005] Check existence of object and it's property at the same time — Cory Patterson <coryp@...>

I run into this from time to time and I was wondering if there is a

10 messages 2010/08/18

[#368076] Shoes 3 released — Steve Klabnik <steve@...>

Hey there everyone. We've just released Shoes 3, "Policeman", to

19 messages 2010/08/19

[#368199] A small problem for arrays — Unc88 Unc88 <unc88@...>

I have 2 array. ar_1, ar_2

11 messages 2010/08/21

[#368343] gem list --remote does not work on windows running ruby 1.9.2p0 — botp <botpena@...>

Title says all.

9 messages 2010/08/24

[#368384] ffi-ncurses 0.3.3 — "Sean O'Halpin" <sean.ohalpin@...>

I've just released version 0.3.3 of the ffi-ncurses gem. This fixes

22 messages 2010/08/25
[#368423] Re: ffi-ncurses 0.3.3 — "R.. Kumar 1.9.1 OSX" <sentinel1879@...> 2010/08/26

Sean O'halpin wrote:

[#368500] Re: ffi-ncurses 0.3.3 — "Sean O'Halpin" <sean.ohalpin@...> 2010/08/27

On Thu, Aug 26, 2010 at 11:20 AM, R.. Kumar 1.9.1 OSX

[#368533] Re: ffi-ncurses 0.3.3 — botp <botpena@...> 2010/08/28

On Fri, Aug 27, 2010 at 10:05 PM, Sean O'Halpin <sean.ohalpin@gmail.com> wrote:

[#368538] Re: ffi-ncurses 0.3.3 — Rahul Kumar <sentinel1879@...> 2010/08/28

[#368546] Re: ffi-ncurses 0.3.3 — botp <botpena@...> 2010/08/28

On Sat, Aug 28, 2010 at 2:34 PM, Rahul Kumar <sentinel1879@gmail.com> wrote:

[#368556] Re: ffi-ncurses 0.3.3 — "Sean O'Halpin" <sean.ohalpin@...> 2010/08/28

On Sat, Aug 28, 2010 at 9:47 AM, botp <botpena@gmail.com> wrote:

[#368623] Re: ffi-ncurses 0.3.3 — Rahul Kumar <sentinel1879@...> 2010/08/30

[#368471] how about Array#collect_until — timr <timrandg@...>

I am wondering if anyone has implemented an Array#collect_until method

14 messages 2010/08/27

[#368506] select tr>3 with nokogiri — Pen Ttt <myocean135@...>

13 messages 2010/08/27

[#368690] Namespaces too looooooong — Iain Barnett <iainspeed@...>

Hi,

18 messages 2010/08/31
[#368692] Re: Namespaces too looooooong — Joel VanderWerf <joelvanderwerf@...> 2010/08/31

On 08/30/2010 05:51 PM, Iain Barnett wrote:

[#368694] Re: Namespaces too looooooong — Iain Barnett <iainspeed@...> 2010/08/31

Re: [].all?{} and [].any?{} Behavior

From: Robert Klemme <shortcutter@...>
Date: 2010-08-01 12:11:28 UTC
List: ruby-talk #366871
2010/7/31 Rick DeNatale <rick.denatale@gmail.com>:
> On Sat, Jul 31, 2010 at 6:07 AM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>>
>>> For your own usage as long as it doesn't mess up some other code you
>>> are using, feel free.
>>
>> I disagree: IMHO it is a bad idea to change such fundamental behavior
>> if only for own code. =A0This opens the door widely for all sorts of
>> bugs and issues. =A0For example, you get used to #all? doing also the
>> emptyness check and get confused when reading other code which of
>> course relies on the regular behavior. =A0Or you forget the "require"
>> for the file that changes semantics of #all? and #any? and receive in
>> turn subtly bugs which might be hard to track down. =A0Even worse, you
>> use library code that in turn uses #all? or #any? without you knowing
>> it and this code suddenly breaks.
>
> Whether or not it's a bad idea, and I tend to agree that it is. =A0I
> said what I did for at least two reasons:
>
> 1) I tend not to be puritanical, just as I wouldn't restrict what
> anyone wanted to do in the privacy of their own homes unless it was
> harmful to others, I think you should be able to write whatever code
> you want to under the same philosophy, whether or not it's harmful to
> you.

I did not want to sound puritanical, just point out that certain
habits are likely to have negative impact over time.  And of course I
do agree that everyone should write their code as they see fit.

> 2) Since one of the most useful ways to learn anything in such a way
> that you remember and internalize it is to make a mistake and realize
> the consequences.

LOL

>>> module Enumerable
>>> =A0def non_vacuous_all?(&b)
>>> =A0 =A0!empty? && all?(&b)
>>> =A0end
>>> end
>>>
>>> [3].all? {|element| element =3D=3D 3 } =A0# =3D> true
>>> [3].all? {|element| element !=3D 3 } =A0# =3D> false
>>>
>>> [].all? {|element| element =3D=3D 3 } =A0 # =3D> true
>>> [].all? {|element| element !=3D 3 } =A0 # =3D> true
>>>
>>> [3].non_vacuous_all? {|element| element =3D=3D 3 } =A0# =3D> true
>>> [3].non_vacuous_all? {|element| element !=3D 3 } =A0# =3D> false
>>>
>>> [].non_vacuous_all? {|element| element =3D=3D 3 } =A0 # =3D> false
>>> [].non_vacuous_all? {|element| element !=3D 3 } =A0 # =3D> false
>>>
>>> [].any? {|element| element =3D=3D 3 } =A0 # =3D> false
>>> [].any? {|element| element !=3D 3 } =A0 # =3D> false
>>>
>>> There may be a better name than non_vacuous_all? but I can't think of o=
ne.
>>
>> I'd rather stick with two method calls because it makes crystall clear
>> what's happening. =A0Also, you may first want to check for emptyness and
>> if else branch based on that knowledge (or the other way round). =A0In
>> other words: often you may want to separate both checks
>
> Here I completely disagree. =A0Extracting commonly used code to a well
> named method is an essential part of writing and maintaining code.

While this is true as a general statement, creating too many methods
with trivial behavior can have a detrimental effect on software
readability and maintainability.  The reason is that by doing this you
create additioanl artifacts that need to be documented and understand
by a reader.  If on the other hand the idiom "!x.empty? && x.all?
{...}" is seen you can directly understand the behavior from three
basic mechanisms (#empty?, && and #all?) without having to resort to
other documentation.  Finding a proper name for the combined method
also needs some careful consideration (as you have shown below).

> for example, I see nothing wrong with the sum method which active
> support adds to Enumerable
>
> =A0# Calculates a sum from the elements. Examples:
> =A0#
> =A0# =A0payments.sum { |p| p.price * p.tax_rate }
> =A0# =A0payments.sum(&:price)
> =A0#
> =A0# The latter is a shortcut for:
> =A0#
> =A0# =A0payments.inject { |sum, p| sum + p.price }
> =A0#
> =A0# It can also calculate the sum without the use of a block.
> =A0#
> =A0# =A0[5, 15, 10].sum # =3D> 30
> =A0# =A0["foo", "bar"].sum # =3D> "foobar"
> =A0# =A0[[1, 2], [3, 1, 5]].sum =3D> [1, 2, 3, 1, 5]
> =A0#
> =A0# The default sum of an empty list is zero. You can override this defa=
ult:
> =A0#
> =A0# =A0[].sum(Payment.new(0)) { |i| i.amount } # =3D> Payment.new(0)
> =A0#
> =A0def sum(identity =3D 0, &block)
> =A0 =A0if block_given?
> =A0 =A0 =A0map(&block).sum(identity)
> =A0 =A0else
> =A0 =A0 =A0inject { |sum, element| sum + element } || identity
> =A0 =A0end
> =A0end

There is at least the point that there is no universal neutral element
and 0 had to be chosen as default here; it's certainly the proper
value for many (most?) cases in practice but one should at least be
aware of this.  If your collection contains strings you would need to
use "" as the neutral element (which strangely enough is called
"identity" here which to me sounds like the identity function which is
something different - but I'm nitpicking).

http://en.wikipedia.org/wiki/Neutral_element
http://en.wikipedia.org/wiki/Identity_function

> Following your argument, this is bad because you might want to use
> inject and + separately.

No, that was not my point.  My point was that for the *same*
collection you may want to separate the emptyness check and the any /
all check that you combine in a single method.  If you combine both in
a single method you cannot evalute results separately (or rather you
cannot use that method if you need the results separately).  This is a
common case if you want to provide user responses that are easier to
understand for a human being.

> But having such methods doesn't prevent you in the least from using
> inject, +, empty?, any? or any other method used to implement a
> slightly more abstract extracted method separately. =A0It does help to
> keep your code DRY and to make it more understandable overall since
> you don't have to re-understand the effect of the separate invocations
> each time you encounter them, as long as you are careful and name the
> abstraction in an 'intention revealing' way.

As I said above, I do not think that creating new methods makes code
more understandable in all cases.

> And doing this also enables changing the implementation of the
> abstraction without holistic changes to the code. =A0Yes I know about de
> Morgan's rules (I have a CS degree granted by a 1970's era Electrical
> Engineering department).

The hint was intended for the OP.  I never assumed you didn't know De
Morgan's rules.

> Placing the implementation in an abstraction
> allows you to do the math proofs/unit testing and refactoring to meet
> particular non-functional requirements in one place, which is a good
> thing.

Right.

> I recently was working on a refactoring a large Rails application
> taken over from another development shop, which had several nasty bugs
> on just this issue of all? returning true for an empty collection. =A0It
> turns out that there are definitely cases where you want to test that
> a collection has at least 1 element and that all of the elements have
> some property. =A0Having said that perhaps a better name for the method
> might be
>
> =A0 at_least_one_and_all?
>
> That might be a tad long, but I'd rather have a longer but more
> intention revealing name, and let one of the several editors I use
> deal with keeping my keystroke count down.

I tend to prefer longer and more telling names as well.  As you said,
modern environments provide plenty of utilities to deal with the nasty
typing gracefully.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

In This Thread