Forum: Ruby new to Ruby - pls help in translating this

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
sam.dela.cruz (Guest)
on 2005-12-09 20:52
(Received via mailing list)
Hi,

I'm starting to use Ruby in one of my projects at work.  I'm coming from
a
Perl background.
In my project I would need to parse a list of numbers (thousands of
them)
and then return the duplicates.  In perl, I can do this:

##### Perl code
%hash = {};
while (<>)
{
    chomp;
        $hash{$_}++;
}

foreach my $key (sort keys %hash)
{
    print "$key: $hash{$key}\n" if ($hash{$key} > 1);
}

I tried to translate this in Ruby, but could not find en equivalent of
$hash{$_}++, this is auto increment.
Can somebody tell me how this is to be done in Ruby?  Or maybe the Ruby
way on how to attack this whole thing.  Thanks.

Regards,
Sam
James G. (Guest)
on 2005-12-09 20:56
(Received via mailing list)
On Dec 9, 2005, at 12:48 PM, Sam Dela C. wrote:

> %hash = {};
>
> I tried to translate this in Ruby, but could not find en equivalent of
> $hash{$_}++, this is auto increment.

See if this gets you going:

   seen = Hash.new(0)
   ARGF.each_line { |line| seen[line.strip] += 1 }

   seen.each { |key, value| puts key if value > 1 }

James Edward G. II
dandiebolt (Guest)
on 2005-12-09 21:10
(Received via mailing list)
use the following to initiate your hash:

  h=Hash.new(0)

  and this to increment on each key:

  h[key] = h[key] + 1
m.fellinger (Guest)
on 2005-12-09 21:18
(Received via mailing list)
Hey,

after looking at your code (wich was a bit hard, since i don't speak
perl) - i
came up with following solution, that doesn't seem very ruby-esque but
works
well for me.
I'm sure someone brings the code down to one line, but i'm a bit too
lazy to
try that :)

file = File.open(ARGV[0]).readlines
h = {}
file.each{|x| (h[x].nil?) ? (h[x] = 1) : (h[x] = h[x]+=1)}
h.each_pair{|k,v| puts "#{k.strip} appears #{v} times" if v >= 2}

Am Freitag, 9. Dezember 2005 19:48 schrieb Sam Dela C.:
logancapaldo (Guest)
on 2005-12-09 21:18
(Received via mailing list)
On Dec 9, 2005, at 1:48 PM, Sam Dela C. wrote:

> %hash = {};
>
hash = Hash.new { |h, k| h[k] = 0 }

while gets
     $_.chomp!
     hash[$_] += 1
end

> I tried to translate this in Ruby, but could not find en equivalent of
> $hash{$_}++, this is auto increment.

Ruby has no auto-increment since variables are more like labels on
objects than containers for objects and numbers are generally
immutable in ruby.
IOW:
x = 3
x++ in ruby would be like typing 3++ which doesn't make sense really.
pat.eyler (Guest)
on 2005-12-09 21:26
(Received via mailing list)
On 12/9/05, Sam Dela C. <removed_email_address@domain.invalid> wrote:
> Hi,
>
> I'm starting to use Ruby in one of my projects at work.  I'm coming from a
> Perl background.
> In my project I would need to parse a list of numbers (thousands of them)
> and then return the duplicates.  In perl, I can do this:
>
[elided perl goo]
>
> I tried to translate this in Ruby, but could not find en equivalent of
> $hash{$_}++, this is auto increment.
> Can somebody tell me how this is to be done in Ruby?

translating from Perl to Ruby seems often to be a bad idea ... a
common idea, but not necessarily a good one.   I'd rather work
with a Ruby solution to a problem than a Rubification of a Perl
solution to a problem.

> Or maybe the Ruby
> way on how to attack this whole thing.  Thanks.

I'm assuming that your list comes from a file (but you can change that
pretty easily in the code below), given that, how about something like:


seen_ary = Array.new

File.open("nums","r").each do |elem|
  print elem if seen_ary.include?(elem)
  seen_ary.push(elem)
end

(there are probably still better ways of doing this though)
mrkode (Guest)
on 2005-12-09 21:26
(Received via mailing list)
well, there is the succ method. and there's a succ! on String, which
could
work in this case, I guess(?), since:

irb(main):023:0> a = "9"
=> "9"
irb(main):024:0> a.succ!
=> "10"
dandiebolt (Guest)
on 2005-12-09 21:35
(Received via mailing list)
>variables are more like labels on objects than containers for objects

  There is something very fundamental about what you are saying here but
I don't quite understand. Could you elaborate on this with perhaps an
example?
wilsonb (Guest)
on 2005-12-09 21:39
(Received via mailing list)
Or, something else to think about.. shamelessly borrowed from this
mailing list a month or so ago..
Wrapped in lame 'gets' code to let you play with it at the command
line.  I presume you'd actually do something cooler with STDIN or a
filename.

class Array
  def dups
     h, d = {}, []; each{|e| h[e] ? (d << h.delete(e)) : (h[e] = e)}; d
  end
end
numbers = []
while n = STDIN.gets.chomp
  break if n == ''
  numbers << n
end
puts numbers.dups

I love reopening Array.

--Wilson.
drbrain (Guest)
on 2005-12-09 21:39
(Received via mailing list)
On Dec 9, 2005, at 11:23 AM, pat eyler wrote:

> [elided perl goo]
>>
>> I tried to translate this in Ruby, but could not find en
>> equivalent of
>> $hash{$_}++, this is auto increment.
>> Can somebody tell me how this is to be done in Ruby?
>
> translating from Perl to Ruby seems often to be a bad idea ... a
> common idea, but not necessarily a good one.   I'd rather work
> with a Ruby solution to a problem than a Rubification of a Perl
> solution to a problem.

Ditto.  I often find that I can make my code close to readable
English and find that a very good thing.

>   seen_ary.push(elem)
> end
>
> (there are probably still better ways of doing this though)

I'll go with:

seen = {}

ARGF.each do |elem|
   print elem if seen.include? elem
   seen[elem] = true
end

--
Eric H. - removed_email_address@domain.invalid - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
mental (Guest)
on 2005-12-09 21:56
(Received via mailing list)
Quoting Sam Dela C. <removed_email_address@domain.invalid>:

> %hash = {};
>
> I tried to translate this in Ruby, but could not find en
> equivalent of $hash{$_}++, this is auto increment.

It also autovivifies the hash element, but Ruby doesn't have
autovivification.

Here's a nearly literal translation to Ruby (the main difference is
that gets is like <STDIN>, not <>):

 hash = {}
 while gets
   chomp
   hash[$_] ||= 0 # vivify
   hash[$_] += 1  # increment
 end

 for key in hash.keys.sort
   print "#{key}: #{hash[key]}\n" if hash[key] > 1
 end

Here's the same thing in a slightly more Ruby-ish style ($_ is
normally avoided in Ruby, even though it's available):

 hash = Hash.new( 0 )
 $stdin.each do |line|
   hash[line.chomp] += 1
 end

 hash.keys.sort.each do |key|
   puts "#{key}: #{hash[key]}" if hash[key] > 1
 end

Here, we create a hash whose default value for uninitialized
elements is 0, rather than nil (nil is like Perl's undef).  Note
that a default value you provide this way is used directly for
every element; it is not copied.

Note also that line.chomp is not the same thing as chomp $line in
Perl; line.chomp returns a new string rather than modifying the
existing one.  The exact equivalent of Perl's chomp $line would be
line.chomp!.

-mental
SimonKroeger (Guest)
on 2005-12-09 22:04
(Received via mailing list)
> I'll go with:
>
> seen = {}
>
> ARGF.each do |elem|
>   print elem if seen.include? elem
>   seen[elem] = true
> end

Thanks, this is how ruby should look like!

here is another one (i like it a bit more
functional style):

quantities = Hash.new{|h, k| h[k]=0}
ARGF.each{|l| quantities[l.chomp.to_i] += 1}
puts quantities.delete_if{|k, v| v <= 1}.keys

cheers

Simon
drbrain (Guest)
on 2005-12-09 22:12
(Received via mailing list)
On Dec 9, 2005, at 11:37 AM, Wilson B. wrote:

>   end
> end

That is far too complicated and impossible to read.  I think we
should be giving new people examples of how to make code clear and
expressive.

> numbers = []
> while n = STDIN.gets.chomp

$ echo -n '' | ruby -e 'p STDIN.gets'
nil

>   break if n == ''
>   numbers << n
> end
> puts numbers.dups
>
> I love reopening Array.

I like clear, expressive code: [ruby-talk:169927]

--
Eric H. - removed_email_address@domain.invalid - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
logancapaldo (Guest)
on 2005-12-09 22:28
(Received via mailing list)
On Dec 9, 2005, at 2:31 PM, Dan D. wrote:

>> variables are more like labels on objects than containers for objects
>
>   There is something very fundamental about what you are saying
> here but I don't quite understand. Could you elaborate on this with
> perhaps an example?
>
>
> ---------------------------------
> Yahoo! Shopping
>  Find Great Deals on Holiday Gifts at Yahoo! Shopping

Well basically, all variables in ruby are references (or pointers,
whatever you want to call them), but they are actually even less
"clingy" than that.

maybe an example will help

x = "a string"
y = x # this doesn't do something like x.copy nor  is it even
equalivalent to typing y = "a string"
          # it more like saying
         #   x =
         #             "a string"
         #   y =

y.upcase! #=> "A STRING"
x                #=> "A STRING"

Just think of varibles as a way to refer to a memory address
If x and y "contain" anythjing it is a memory address and they both
have the same memory.

Basically they are pointers. Except when they aren't. Fixnums for
instance are stored as values. As are symbols. I think I might have
over complicated this. Maybe someone else can explain it better.
logancapaldo (Guest)
on 2005-12-09 22:32
(Received via mailing list)
On Dec 9, 2005, at 2:24 PM, JB Eriksson wrote:

> well, there is the succ method. and there's a succ! on String,
> which could
> work in this case, I guess(?), since:
>
> irb(main):023:0> a = "9"
> => "9"
> irb(main):024:0> a.succ!
> => "10"

Well, if it was a string. The problem is fixnums are immediate
values. ++ would therefore be an operation on the variable a, rather
than the object that a points at.
sam.dela.cruz (Guest)
on 2005-12-09 22:57
(Received via mailing list)
Thank you all.  I learned a little bit more ruby.  And yes, I want to do
things the Ruby way, but I just can't help to think perl still and then
translate to ruby.  This is very similar to when I came to the U.S.  and
all of a sudden I have to talk in English, I still think in my original
language(Tagalog) and then translate it in English and it doesn't always
come up the way English speakers would say it.  But I get the point
accross.  But yes, going back to Ruby, I'd like to learn things the Ruby
way and so far I have good things to say about Ruby.  One, it being so
readable, and second I think it is easier to learn especially the OOP
part
of it.  In perl OOP is a big hurdle.

Here's what I decided on using (looks more like what I originally posted
in perl):

hash = Hash.new(0)
File.open(ARGV[0],"r").each do |line|
    hash[line.chomp] += 1
end

hash.keys.sort.each do |key|
    puts "#{key}: #{hash[key]}" if hash[key] > 1
end

Regards,
Sam Dela C.









Eric H. <removed_email_address@domain.invalid>
12/09/2005 11:39 AM
Please respond to
removed_email_address@domain.invalid


To
removed_email_address@domain.invalid (ruby-talk ML)
cc

Subject
Re: new to Ruby - pls help in translating this
Classification







On Dec 9, 2005, at 11:23 AM, pat eyler wrote:

> [elided perl goo]
>>
>> I tried to translate this in Ruby, but could not find en
>> equivalent of
>> $hash{$_}++, this is auto increment.
>> Can somebody tell me how this is to be done in Ruby?
>
> translating from Perl to Ruby seems often to be a bad idea ... a
> common idea, but not necessarily a good one.   I'd rather work
> with a Ruby solution to a problem than a Rubification of a Perl
> solution to a problem.

Ditto.  I often find that I can make my code close to readable
English and find that a very good thing.

>   seen_ary.push(elem)
> end
>
> (there are probably still better ways of doing this though)

I'll go with:

seen = {}

ARGF.each do |elem|
   print elem if seen.include? elem
   seen[elem] = true
end

--
Eric H. - removed_email_address@domain.invalid - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
James G. (Guest)
on 2005-12-09 23:05
(Received via mailing list)
On Dec 9, 2005, at 2:53 PM, Sam Dela C. wrote:

> Here's what I decided on using (looks more like what I originally
> posted
> in perl):
>
> hash = Hash.new(0)
> File.open(ARGV[0],"r").each do |line|

Replacing the above with:

ARGF.each do |line|

Is the same thing, but more powerful.  You could feed it multiple
files at once, or use it in pipelined processing.  Just FYI.

James Edward G. II
mental (Guest)
on 2005-12-09 23:05
(Received via mailing list)
Quoting Sam Dela C. <removed_email_address@domain.invalid>:

> end
One caution here -- if you want the file handle to be closed when
you are finished, you must do either:

 stream = File.open(ARGV[0],"r")
 stream.each do |line|
   hash[line.chomp] += 1
 end
 stream.close

or (this is often better because it will automatically close the
handle even if there's an exception):

 File.open(ARGV[0],"r") do |stream|
   stream.each do |line|
     hash[line.chomp] += 1
   end
 end

-mental
sam.dela.cruz (Guest)
on 2005-12-09 23:13
(Received via mailing list)
Yes, file handles must be closed.  I learned that the hard way on one of
my projects before.  Thanks again.
Sam









removed_email_address@domain.invalid
12/09/2005 01:02 PM

To
Sam Dela C./SVL/SC/PHILIPS@PHILIPS
cc

Subject
Re: new to Ruby - pls help in translating this
Classification







Quoting Sam Dela C. <removed_email_address@domain.invalid>:

> end
One caution here -- if you want the file handle to be closed when
you are finished, you must do either:

 stream = File.open(ARGV[0],"r")
 stream.each do |line|
   hash[line.chomp] += 1
 end
 stream.close

or (this is often better because it will automatically close the
handle even if there's an exception):

 File.open(ARGV[0],"r") do |stream|
   stream.each do |line|
     hash[line.chomp] += 1
   end
 end

-mental
James G. (Guest)
on 2005-12-09 23:17
(Received via mailing list)
On Dec 9, 2005, at 3:02 PM, removed_email_address@domain.invalid wrote:

>> hash.keys.sort.each do |key|
>  stream.close
>
> or (this is often better because it will automatically close the
> handle even if there's an exception):
>
>  File.open(ARGV[0],"r") do |stream|
>    stream.each do |line|
>      hash[line.chomp] += 1
>    end
>  end

Or:

File.foreach(ARGV.first) do |line|
   # ...
end

James Edward G. II
m.fellinger (Guest)
on 2005-12-09 23:21
(Received via mailing list)
Am Freitag, 9. Dezember 2005 21:31 schrieb Logan C.:
> Well, if it was a string. The problem is fixnums are immediate
> values. ++ would therefore be an operation on the variable a, rather
> than the object that a points at.

well, there is a+=1
and a++ would be a+! imho...
mental (Guest)
on 2005-12-10 00:14
(Received via mailing list)
Quoting James Edward G. II <removed_email_address@domain.invalid>:

> Replacing the above with:
>
> ARGF.each do |line|
>
> Is the same thing, but more powerful.  You could feed it multiple
> files at once, or use it in pipelined processing.  Just FYI.

Ah, thank you.  I couldn't remember how to do that.  That's closer
in spirit to his original Perl fragment, which used <>.

Perl:

 while (<>) {
   ...
 }

Ruby:

 while ARGF.gets
   ...
 end

(not that I'm advocating $_)

-mental
penryu (Guest)
on 2005-12-10 00:51
(Received via mailing list)
Sam Dela C. <removed_email_address@domain.invalid> wrote:
> I'm starting to use Ruby in one of my projects at work.  I'm
> coming from a Perl background.
>
> In my project I would need to parse a list of numbers
> (thousands of them) and then return the duplicates.  In perl,
> I can do this:
>
> ##### Perl code
> %hash = {};

That doesn't do what you think it does.  In Perl, the {} here
will create a hash reference, which is a scalar datatype.
You're assigning it to a %hash variable, which accepts LIST
data.  This ends up being the same as:

  %hash = ( {} );

Which creates a hash named %hash, stringifies the hash reference
and uses it as a key, and without any additional values to work
with, uses undef as a value for the key.

To see this in action:

    use Data::Dumper;
    my %hash = {};
    print Dumper( \%hash );

Compare to:

    use Data::Dumper;
    my %hash = ();
    print Dumper( \%hash );

Yes. You want to use parentheses.

> while (<>)
> {
>     chomp;
>         $hash{$_}++;
> }
>
> foreach my $key (sort keys %hash)
> {
>     print "$key: $hash{$key}\n" if ($hash{$key} > 1);
> }


HTH,
Tim H.
pat.eyler (Guest)
on 2005-12-10 01:44
(Received via mailing list)
On 12/9/05, Eric H. <removed_email_address@domain.invalid> wrote:
> > (there are probably still better ways of doing this though)
>
> I'll go with:
>
> seen = {}
>
> ARGF.each do |elem|
>    print elem if seen.include? elem
>    seen[elem] = true
> end
>

And yes, this way is much better (I knew it would be but, it's nice to
see just how much better):


require 'benchmark'
n = 100

# build a big array with lots of duplication
@nums = (1..1_000).to_a
(1..1_000).step(17) {|num| @nums.push(num) }


# set up two versions of our rubylike version
# the original perl-like version and the
# final version
def as_ary
  seen_ary = Array.new

  @nums.each do |elem|
    $stderr.print elem if seen_ary.include?(elem)
    seen_ary.push(elem)
  end
end

def as_hash
  seen = Hash.new

  @nums.each do |elem|
    $stderr.print elem if seen.include?(elem)
    seen[elem] = true
  end
end

# then benchmark them to see how they perform
Benchmark.bm(10) do |x|
  x.report("as Array") {for i in 1..n; as_ary; end }
  x.report("as Hash") {for i in 1..n; as_hash; end }
end

# and the results are:
# ./print_dups.rb 2> /dev/null
#                user     system      total        real
#as Array   12.690000   0.030000  12.720000 ( 13.940651)
#as Hash     0.230000   0.000000   0.230000 (  0.613217)
sam.dela.cruz (Guest)
on 2005-12-10 01:57
(Received via mailing list)
Tim,

Sorry and yes you are right.  This block of code came from a big program
I
have.  I had to modify some stuff to simplify before sending this out to
the mailing list but forgot to change the bracket - { to parentheis - (.
Originally I was using a reference to a hash.  So sorry for those who
got
confused.  Thanks for pointing this out.

Sam









Tim H. <removed_email_address@domain.invalid>
12/09/2005 02:04 PM
Please respond to
removed_email_address@domain.invalid


To
removed_email_address@domain.invalid (ruby-talk ML)
cc

Subject
Re: new to Ruby - pls help in translating this
Classification







List-Owner: <mailto:removed_email_address@domain.invalid>
List-Help: <mailto:removed_email_address@domain.invalid?body=help>
List-Unsubscribe: <mailto:removed_email_address@domain.invalid?body=unsubscribe>

Sam Dela C. <removed_email_address@domain.invalid> wrote:
> I'm starting to use Ruby in one of my projects at work.  I'm
> coming from a Perl background.
>
> In my project I would need to parse a list of numbers
> (thousands of them) and then return the duplicates.  In perl,
> I can do this:
>
> ##### Perl code
> %hash = {};

That doesn't do what you think it does.  In Perl, the {} here
will create a hash reference, which is a scalar datatype.
You're assigning it to a %hash variable, which accepts LIST
data.  This ends up being the same as:

  %hash = ( {} );

Which creates a hash named %hash, stringifies the hash reference
and uses it as a key, and without any additional values to work
with, uses undef as a value for the key.

To see this in action:

    use Data::Dumper;
    my %hash = {};
    print Dumper( \%hash );

Compare to:

    use Data::Dumper;
    my %hash = ();
    print Dumper( \%hash );

Yes. You want to use parentheses.

> while (<>)
> {
>     chomp;
>         $hash{$_}++;
> }
>
> foreach my $key (sort keys %hash)
> {
>     print "$key: $hash{$key}\n" if ($hash{$key} > 1);
> }


HTH,
Tim H.
corey (Guest)
on 2005-12-10 02:17
(Received via mailing list)
Or, in keeping with the DRY and KISS principles I humbly submit the
lowly shell command, uniq, instead of Ruby at all:

$ uniq -d <file>

e.g.

/tmp corey$ cat foo
1
1
2
3
3
4
/tmp corey$ uniq -d foo
1
3
/tmp corey$

Although, it was interesting to see the diversity of Ruby solutions.

Corey
pat.eyler (Guest)
on 2005-12-10 03:06
(Received via mailing list)
On 12/9/05, Corey J. <removed_email_address@domain.invalid> wrote:
> 2
> 3
> 3
> 4
> /tmp corey$ uniq -d foo
> 1
> 3
> /tmp corey$
>

but it doesn't always work:
pate@linux:~/scratch> cat nums
1
2
3
4
4
5
6
5
7
pate@linux:~/scratch> uniq -d files
uniq: files: No such file or directory
pate@linux:~/scratch> uniq -d nums
4
pate@linux:~/scratch>


you need to do:
pate@linux:~/scratch> sort -n nums | uniq -d
4
5


but if this is supposed to be part of a larger program, shell
is probably the wrong way to go about it.
perrin (Guest)
on 2005-12-10 03:51
(Received via mailing list)
On Sat, Dec 10, 2005 at 10:03:06AM +0900, pat eyler wrote:
>
> but if this is supposed to be part of a larger program, shell
> is probably the wrong way to go about it.

Why?  Backticks are great.

--
Chad P. [ CCD CopyWrite | http://ccd.apotheon.org ]

unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.
gregory.t.brown (Guest)
on 2005-12-10 04:40
(Received via mailing list)
On 12/9/05, Chad P. <removed_email_address@domain.invalid> wrote:

> Why?  Backticks are great.

Platform independence?

Granted I get away with a lot via MingGW, but if you want to code on
Linux / OS X and work for people who want the code to run on
Windows... well... platform independance is important

Though it does of course depend what you're working on. :)
perrin (Guest)
on 2005-12-10 09:49
(Received via mailing list)
On Sat, Dec 10, 2005 at 11:37:02AM +0900, Gregory B. wrote:
> Though it does of course depend what you're working on. :)
Good point.  Okay, you win.

. . . except when I'm writing unix administration scripts.

--
Chad P. [ CCD CopyWrite | http://ccd.apotheon.org ]

unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.
gregory.t.brown (Guest)
on 2005-12-11 06:55
(Received via mailing list)
On 12/10/05, Chad P. <removed_email_address@domain.invalid> wrote:

> Good point.  Okay, you win.
>
> . . . except when I'm writing unix administration scripts.

Of COURSE! :)
You can even port a lot of those straight to windows via MSys and MinGW.
(Or it's ugly cousin Cygwin)
w_a_x_man (Guest)
on 2005-12-11 17:36
(Received via mailing list)
Eric H. wrote:

> seen = {}
>
> ARGF.each do |elem|
>    print elem if seen.include? elem
>    seen[elem] = true
> end

seen = {}
while s = gets
  print s if seen.key? s
  seen[ s ] = nil
end

Or:

seen = Hash.new(0)
while s = gets
  print s if ( seen[s] += 1 ) > 1
end
William J. (Guest)
on 2005-12-11 18:27
(Received via mailing list)
William J. wrote:
> while s = gets
>   print s if seen.key? s
>   seen[ s ] = nil
> end
>
> Or:
>
> seen = Hash.new(0)
> while s = gets
>   print s if ( seen[s] += 1 ) > 1
> end

seen, s = Hash.new(0)
print s if ( seen[s] += 1 ) > 1 while s = gets
William J. (Guest)
on 2005-12-11 18:27
(Received via mailing list)
William J. wrote:
> > seen = {}
> > end
>
> seen, s = Hash.new(0)
> print s if ( seen[s] += 1 ) > 1 while s = gets

seen = Hash.new(0)
print   if ( seen[$_] += 1 ) > 1   while gets
Eric H. (Guest)
on 2005-12-12 09:06
(Received via mailing list)
On Dec 11, 2005, at 7:27 AM, William J. wrote:

>>>
>>>   print s if ( seen[s] += 1 ) > 1
>>> end

This is starting to get pointlessly obfuscated.

>> seen, s = Hash.new(0)
>> print s if ( seen[s] += 1 ) > 1 while s = gets

Stop.  You're making things hard to read for the new people.

> seen = Hash.new(0)
> print   if ( seen[$_] += 1 ) > 1   while gets

I can't read that so I don't know how you expect someone new to Ruby
to read it.

I've never seen 'ruby golf' do anything positive.  I find it
especially inappropriate to be performing these vile manipulations
while trying to show new people idiomatic Ruby like they asked for:

> Can somebody tell me how this is to be done in Ruby?  Or maybe the
> Ruby
> way on how to attack this whole thing.  Thanks.

--
Eric H. - removed_email_address@domain.invalid - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
William J. (Guest)
on 2005-12-12 21:31
(Received via mailing list)
Eric H. wrote:
> >>>>    seen[elem] = true
> >>> seen = Hash.new(0)
>
> > seen = Hash.new(0)
> > print   if ( seen[$_] += 1 ) > 1   while gets
>
> I can't read that so I don't know how you expect someone new to Ruby
> to read it.

A standard Ruby idiom: instead of 3 lines . . .

  if test
    print "ok"
  end

.. . . 1 line:

  print "ok"  if test

Also, as in Awk, "print" with no argument prints the line just read.
Very simple.  If one needs to explicitly refer to the line just read,
one uses "$_".

And "while gets" is simpler and clearer than "ARGF.each do |elem|".
This topic is locked and can not be replied to.