Forum: Ruby Newbie: Printing hex formatted string using printf

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.
Wes G. (Guest)
on 2006-03-22 01:00
I am trying to print the hex representation of a string using printf.

  $stderr.print "Magic number is: #{magic_number}.printf('%X')\n"

Why does this not work?

The output is:

Magic number is: %PDF-1.3.printf('%X')
Wes G. (Guest)
on 2006-03-22 01:02
In irb, I try to figure out why this doesn't work:

irb(main):001:0> x=3
=> 3
irb(main):002:0> 3.printf("%X")
NoMethodError: private method `printf' called for 3:Fixnum
        from (irb):2
irb(main):003:0> x.printf("%X")
NoMethodError: private method `printf' called for 3:Fixnum
        from (irb):3
irb(main):004:0>

Why does printf act like a private method here?

wg

Wes G. wrote:
> I am trying to print the hex representation of a string using printf.
>
>   $stderr.print "Magic number is: #{magic_number}.printf('%X')\n"
>
> Why does this not work?
>
> The output is:
>
> Magic number is: %PDF-1.3.printf('%X')
Matthew M. (Guest)
on 2006-03-22 01:10
(Received via mailing list)
> Why does printf act like a private method here?

printf is not a method on Fixnum. The error is confusing (except that
the first word is "NoMethodError").

If you want to use printf, just do:

$stderr.printf "Magic number is: %X\n", magic_number

Or, more ruby-ish:

puts "Magic number is: #{magic_number.to_s(16)}"
Mike S. (Guest)
on 2006-03-22 01:11
(Received via mailing list)
On 21-Mar-06, at 6:03 PM, Wes G. wrote:

> irb(main):004:0>
>
> Why does printf act like a private method here?

Because printf is a method of Kernel

ratdog:~ mike$ irb
irb(main):001:0> x = 3
=> 3
irb(main):002:0> Kernel.printf('%X', x)
3=> nil
irb(main):003:0> 3.methods.include?('printf')
=> false
irb(main):004:0> Kernel.methods.include?('printf')
=> true

Does this help?

Mike

>> The output is:
>>
>> Magic number is: %PDF-1.3.printf('%X')
>
>
> --
> Posted via http://www.ruby-forum.com/.
>
>

--

Mike S. <removed_email_address@domain.invalid>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
Mike S. (Guest)
on 2006-03-22 01:13
(Received via mailing list)
On 21-Mar-06, at 6:11 PM, Mike S. wrote:

> => true
>

And for a Fixnum printf is indeed private:

ratdog:~ mike$ irb
irb(main):001:0> 3.private_methods
=> ["select", "lambda", "local_variables", "chomp", "raise", "sub!",
"Array", "print", "trap", "method_missing", "format", "exit!",
"at_exit", "readlines", "set_trace_func", "autoload", "system",
"getc", "initialize_copy", "split", "fail", "putc", "gsub!",
"iterator?", "catch", "p", "remove_instance_variable", "sleep",
"sub", "syscall", "callcc", "Integer", "fork", "srand",
"irb_binding", "singleton_method_removed", "caller", "chop!", "puts",
"scan", "autoload?", "binding", "block_given?", "throw", "warn", "`",
"gsub", "loop", "Float", "open", "singleton_method_undefined",
"rand", "exit", "chomp!", "gets", "trace_var", "load", "exec",
"global_variables", "proc", "initialize", "chop", "printf", "String",
"test", "sprintf", "abort", "readline", "untrace_var", "require",
"eval"]


--

Mike S. <removed_email_address@domain.invalid>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
Wes G. (Guest)
on 2006-03-22 01:22
If I do

  puts "Magic number is: #{magic_number.to_s(16)}"

I get

  wrong number of arguments (1 for 0)

wg

Matthew M. wrote:
>> Why does printf act like a private method here?
>
> printf is not a method on Fixnum. The error is confusing (except that
> the first word is "NoMethodError").
>
> If you want to use printf, just do:
>
> $stderr.printf "Magic number is: %X\n", magic_number
>
> Or, more ruby-ish:
>
> puts "Magic number is: #{magic_number.to_s(16)}"
Matthew M. (Guest)
on 2006-03-22 01:30
(Received via mailing list)
irb:

> 123.to_s(16)
=> "7b"
Mike S. (Guest)
on 2006-03-22 01:33
(Received via mailing list)
On 21-Mar-06, at 6:22 PM, Wes G. wrote:

> If I do
>
>   puts "Magic number is: #{magic_number.to_s(16)}"
>
> I get
>
>   wrong number of arguments (1 for 0)
>
> wg

What is in magic_number?  If it's something other than an integer-y
number then the conversion base is not expected e.g.

ratdog:~ mike$ /usr/bin/irb
irb(main):001:0> magic_number = 3
=> 3
irb(main):002:0> puts "Magic number is: #{magic_number.to_s(16)}"
Magic number is: 3
=> nil
irb(main):003:0> "foo".to_s(16)
ArgumentError: wrong number of arguments (1 for 0)
         from (irb):3:in `to_s'
         from (irb):3
irb(main):004:0> 123.4.to_s(16)
ArgumentError: wrong number of arguments (1 for 0)
         from (irb):4:in `to_s'
         from (irb):4

Mike


--

Mike S. <removed_email_address@domain.invalid>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
Wes G. (Guest)
on 2006-03-22 01:38
magic_number is a string returned by IO.read or IO.sysread

I wanted it to be raw bytes but couldn't find a way to get them.

Wes

Mike S. wrote:
> On 21-Mar-06, at 6:22 PM, Wes G. wrote:
>
>> If I do
>>
>>   puts "Magic number is: #{magic_number.to_s(16)}"
>>
>> I get
>>
>>   wrong number of arguments (1 for 0)
>>
>> wg
>
> What is in magic_number?  If it's something other than an integer-y
> number then the conversion base is not expected e.g.
>
> ratdog:~ mike$ /usr/bin/irb
> irb(main):001:0> magic_number = 3
> => 3
> irb(main):002:0> puts "Magic number is: #{magic_number.to_s(16)}"
> Magic number is: 3
> => nil
> irb(main):003:0> "foo".to_s(16)
> ArgumentError: wrong number of arguments (1 for 0)
>          from (irb):3:in `to_s'
>          from (irb):3
> irb(main):004:0> 123.4.to_s(16)
> ArgumentError: wrong number of arguments (1 for 0)
>          from (irb):4:in `to_s'
>          from (irb):4
>
> Mike
>
>
> --
>
> Mike S. <removed_email_address@domain.invalid>
> http://www.stok.ca/~mike/
>
> The "`Stok' disclaimers" apply.
This topic is locked and can not be replied to.