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’)
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’)
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’)
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)}”
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. [email protected]
http://www.stok.ca/~mike/
The “`Stok’ disclaimers” apply.
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. [email protected]
http://www.stok.ca/~mike/
The “`Stok’ disclaimers” apply.
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)}”
irb:
123.to_s(16)
=> “7b”
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. [email protected]
http://www.stok.ca/~mike/
The “`Stok’ disclaimers” apply.
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:into_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):4Mike
–
Mike S. [email protected]
Mike StokThe “`Stok’ disclaimers” apply.
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