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.
E9488ef674513283e2822ba162559131?d=identicon&s=25 sam.dela.cruz (Guest)
on 2005-12-09 19: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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-09 19:56
(Received via mailing list)
On Dec 9, 2005, at 12:48 PM, Sam Dela Cruz 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 Gray II
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 dandiebolt (Guest)
on 2005-12-09 20: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
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 m.fellinger (Guest)
on 2005-12-09 20: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 Cruz:
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 logancapaldo (Guest)
on 2005-12-09 20:18
(Received via mailing list)
On Dec 9, 2005, at 1:48 PM, Sam Dela Cruz 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.
Bb6ecee0238ef2461bef3416722b35c5?d=identicon&s=25 pat.eyler (Guest)
on 2005-12-09 20:26
(Received via mailing list)
On 12/9/05, Sam Dela Cruz <sam.dela.cruz@philips.com> 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)
7cf9493614c47ce48f8a56407a050085?d=identicon&s=25 mrkode (Guest)
on 2005-12-09 20: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"
Ad97b577f331ae29ed90da5751f2e44f?d=identicon&s=25 dandiebolt (Guest)
on 2005-12-09 20: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?
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 wilsonb (Guest)
on 2005-12-09 20: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.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 drbrain (Guest)
on 2005-12-09 20: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 Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-12-09 20:56
(Received via mailing list)
Quoting Sam Dela Cruz <sam.dela.cruz@philips.com>:

> %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
A9c4658e9e475e13d790ae419acf01b6?d=identicon&s=25 SimonKroeger (Guest)
on 2005-12-09 21: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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 drbrain (Guest)
on 2005-12-09 21:12
(Received via mailing list)
On Dec 9, 2005, at 11:37 AM, Wilson Bilkovich 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 Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 logancapaldo (Guest)
on 2005-12-09 21:28
(Received via mailing list)
On Dec 9, 2005, at 2:31 PM, Dan Diebolt 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.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 logancapaldo (Guest)
on 2005-12-09 21: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.
E9488ef674513283e2822ba162559131?d=identicon&s=25 sam.dela.cruz (Guest)
on 2005-12-09 21: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 Cruz









Eric Hodel <drbrain@segment7.net>
12/09/2005 11:39 AM
Please respond to
ruby-talk@ruby-lang.org


To
ruby-talk@ruby-lang.org (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 Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-09 22:05
(Received via mailing list)
On Dec 9, 2005, at 2:53 PM, Sam Dela Cruz 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 Gray II
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-12-09 22:05
(Received via mailing list)
Quoting Sam Dela Cruz <sam.dela.cruz@philips.com>:

> 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
E9488ef674513283e2822ba162559131?d=identicon&s=25 sam.dela.cruz (Guest)
on 2005-12-09 22: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









mental@rydia.net
12/09/2005 01:02 PM

To
Sam Dela Cruz/SVL/SC/PHILIPS@PHILIPS
cc

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







Quoting Sam Dela Cruz <sam.dela.cruz@philips.com>:

> 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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-09 22:17
(Received via mailing list)
On Dec 9, 2005, at 3:02 PM, mental@rydia.net 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 Gray II
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 m.fellinger (Guest)
on 2005-12-09 22:21
(Received via mailing list)
Am Freitag, 9. Dezember 2005 21:31 schrieb Logan Capaldo:
> 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...
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 mental (Guest)
on 2005-12-09 23:14
(Received via mailing list)
Quoting James Edward Gray II <james@grayproductions.net>:

> 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
018f1aa74fcc154beaa6408951d10776?d=identicon&s=25 penryu (Guest)
on 2005-12-09 23:51
(Received via mailing list)
Sam Dela Cruz <sam.dela.cruz@philips.com> 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 Hammerquist
Bb6ecee0238ef2461bef3416722b35c5?d=identicon&s=25 pat.eyler (Guest)
on 2005-12-10 00:44
(Received via mailing list)
On 12/9/05, Eric Hodel <drbrain@segment7.net> 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)
E9488ef674513283e2822ba162559131?d=identicon&s=25 sam.dela.cruz (Guest)
on 2005-12-10 00: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 Hammerquist <penryu@saiyix.ath.cx>
12/09/2005 02:04 PM
Please respond to
ruby-talk@ruby-lang.org


To
ruby-talk@ruby-lang.org (ruby-talk ML)
cc

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







List-Owner: <mailto:ruby-talk-admin@ruby-lang.org>
List-Help: <mailto:ruby-talk-ctl@ruby-lang.org?body=help>
List-Unsubscribe: <mailto:ruby-talk-ctl@ruby-lang.org?body=unsubscribe>

Sam Dela Cruz <sam.dela.cruz@philips.com> 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 Hammerquist
310a5a16e39e2b381e102f2b1e245856?d=identicon&s=25 corey (Guest)
on 2005-12-10 01: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
Bb6ecee0238ef2461bef3416722b35c5?d=identicon&s=25 pat.eyler (Guest)
on 2005-12-10 02:06
(Received via mailing list)
On 12/9/05, Corey Jewett <corey@syntheticplayground.com> 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.
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 perrin (Guest)
on 2005-12-10 02: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 Perrin [ 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.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-12-10 03:40
(Received via mailing list)
On 12/9/05, Chad Perrin <perrin@apotheon.com> 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. :)
Fd22ee3cfc7dac283ce8e451af324f7d?d=identicon&s=25 perrin (Guest)
on 2005-12-10 08:49
(Received via mailing list)
On Sat, Dec 10, 2005 at 11:37:02AM +0900, Gregory Brown 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 Perrin [ 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.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 gregory.t.brown (Guest)
on 2005-12-11 05:55
(Received via mailing list)
On 12/10/05, Chad Perrin <perrin@apotheon.com> 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)
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 w_a_x_man (Guest)
on 2005-12-11 16:36
(Received via mailing list)
Eric Hodel 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
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2005-12-11 17:27
(Received via mailing list)
William James 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
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2005-12-11 17:27
(Received via mailing list)
William James 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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2005-12-12 08:06
(Received via mailing list)
On Dec 11, 2005, at 7:27 AM, William James 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 Hodel - drbrain@segment7.net - http://segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2005-12-12 20:31
(Received via mailing list)
Eric Hodel 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.