irb(main):021:0> hex_ip = “c0a80167”
=> “c0a80167”
irb(main):022:0> ip = hex_ip.scan(/…/).map {|i| i.to_i(16)}.join(".")
=> “192.168.1.103”
martin
irb(main):021:0> hex_ip = “c0a80167”
=> “c0a80167”
irb(main):022:0> ip = hex_ip.scan(/…/).map {|i| i.to_i(16)}.join(".")
=> “192.168.1.103”
martin
On 18:28 Wed 06 Dec , Martin DeMello wrote:
irb(main):021:0> hex_ip = “c0a80167”
=> “c0a80167”
irb(main):022:0> ip = hex_ip.scan(/…/).map {|i| i.to_i(16)}.join(".")
=> “192.168.1.103”
irb(main):070:0> [hex_ip.hex].pack(‘N’).unpack(‘C*’).join(’.’)
=> “192.168.1.103”
Not much, but it’s a start.
ben
On 12/6/06, Martin DeMello [email protected] wrote:
irb(main):021:0> hex_ip = “c0a80167”
=> “c0a80167”
irb(main):022:0> ip = hex_ip.scan(/…/).map {|i| i.to_i(16)}.join(“.”)
=> “192.168.1.103”
hex_ip.gsub(/…/) {|s| “#{s.hex}.”}[0…-2]
The deletion of the trailing ‘.’ is so ugly… but it’s a leetle bit
shorter
Cheers,
Max
Martin DeMello wrote:
irb(main):021:0> hex_ip = “c0a80167”
=> “c0a80167”
irb(main):022:0> ip = hex_ip.scan(/…/).map {|i| i.to_i(16)}.join(".")
=> “192.168.1.103”
ip = [hex_ip].pack(“H*”).unpack(“C*”).join(".")
=> “192.168.1.103”
Max M. schrieb:
On 12/6/06, Martin DeMello [email protected] wrote:
irb(main):021:0> hex_ip = “c0a80167”
=> “c0a80167”
irb(main):022:0> ip = hex_ip.scan(/…/).map {|i| i.to_i(16)}.join(“.”)
=> “192.168.1.103”hex_ip.gsub(/…/) {|s| “#{s.hex}.”}[0…-2]
The deletion of the trailing ‘.’ is so ugly… but it’s a leetle bit
shorter
String#hex is a nice idea.
hex_ip.scan(/…/).map{|s|s.hex}*“.”
Regards,
Pit
hex_ip.gsub(/…/) {|s| “#{s.hex}.”}[0…-2]
The deletion of the trailing ‘.’ is so ugly… but it’s a leetle bit
shorter
Shorter still :
hex_ip.scan(/…/).map{|i|i.hex}*“.”
–
Regards,
Carl D. | 29degrees Ltd
29degrees.co.uk - Bespoke Web Application Development.
codegolf.com - Can you craft the smallest code?
Max M. wrote:
The deletion of the trailing ‘.’ is so ugly… but it’s a leetle bit
shorter
The best so far :). Further refining:
hex_ip.gsub(/…/) {"#{$&.hex}."}.chop
Or, based on Carlos’ optimisation (and I use the term very loosely in
the context of golfing):hex_ip.scan(/…/).map{$&.hex}*“.”
Saves another two characters… and looks horribly perlish.
You’ve broken it
“c0a80167”.scan(/…/).map{$&.hex}*“.”
=> “103.103.103.103”
$& holds the last match of the regex, not the current iteration of map.
On that note, it's a bit of a shame that $_ doesn't work more like it does in perl. It would be very useful when golfing to have a variable that holds the parameters passed to a block without having to explicitly include an argument list to receive them.(Although, I suppose that variable would have to be an array to receive
multiple arguments, so the advantage would be lost because of the extra
bytes needed to get values out of that array.)
Oh well. Ruby is still good for golfing, it’s winning a good few of the
challenges on codegolf.com.
–
Regards,
Carl D. | 29degrees Ltd
29degrees.co.uk - Bespoke Web Application Development.
codegolf.com - Can you craft the smallest code?
Shorter still :
hex_ip.scan(/…/).map{|i|i.hex}*"."
Or, based on Carlos’ optimisation (and I use the term very loosely in
the context of golfing):
hex_ip.scan(/…/).map{$&.hex}*"."
Saves another two characters… and looks horribly perlish.
Max
On 12/6/06, Carl D. [email protected] wrote:
Or, based on Carlos’ optimisation (and I use the term very loosely in
the context of golfing):hex_ip.scan(/…/).map{$&.hex}*“.”
Saves another two characters… and looks horribly perlish.
You’ve broken it
So I have. I guess I need to start writing unit tests for my golfing
efforts. What a sad state of affairs…
Anyway, here’s another one (with a little help):
require ‘scanf’
hex_ip.scanf('%2x’4)“.”
=> “192.168.1.103”
max
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs