I am getting the following as a result of a script I run.
#<USPS::Address:0x7fafdfa53b38 @zip5=“32935”, @city=“MELBOURNE”,
@address2=“1000 N WICKHAM RD”, @zip4=“8937”, @state=“FL”>
in a variable named result.
How can I access just the Address value and print it on the screen?
qaswm
August 26, 2011, 9:51am
2
Le 26/08/2011 09:33, QAS WM a écrit :
I am getting the following as a result of a script I run.
#<USPS::Address:0x7fafdfa53b38 @zip5=“32935”, @city=“MELBOURNE”,
@address2=“1000 N WICKHAM RD”, @zip4=“8937”, @state=“FL”>
in a variable named result.
How can I access just the Address value and print it on the screen?
puts result.id
JCLL
qaswm
August 26, 2011, 9:58am
3
Le 26/08/2011 09:50, Jean-Christophe Le Lann a écrit :
puts result.id
JCLL
Sorry. That is wrong, and .id is deprecated (use .object_id).
Anyway, I am not sure what you need really
class Ups
def initialize town,guy
@town ,guy=town,guy
end
end
result = Ups.new(:madrid,:diego)
puts result #Ups:0x9a46f10 <== that number ?
puts result.object_id #80885640 <== or this one ?
qaswm
August 26, 2011, 10:15am
4
On Fri, Aug 26, 2011 at 2:33 AM, QAS WM [email protected] wrote:
Posted via http://www.ruby-forum.com/ .
What version/implementation of Ruby are you using? That’s a curious
inspection, none of mine label it as the address.
Anyway, that’s the address of the object in memory, not the location
you’re
sending the package, you probably want @address2 .
Given that I don’t know how your object works, I can’t say for sure.
Usually
people will have a getter named the same as the variable, so you could
just
say result.address2. If not, the only think you can definitely do would
be a
violation of encapsulation, but you could say
result.instance_variable_get(‘@address2 ’), but I recommend finding the
actual getter (note, if you don’t know your object’s APIs, and they
aren’t
well documented, Pry is a good tool for exploring them
http://pry.github.com/ ).
qaswm
August 26, 2011, 12:16pm
5
I am actually trying to use this gist
and get the correct address2 for a certain address.
Not sure how to get it out from the response which comes back in the
form I provided above.
qaswm
August 26, 2011, 12:22pm
6
Here’s the relevant line:
class Address
[:firm_name, :address1, :address2, :city, :state, :zip5, :zip4].each
do |a|
attr_accessor a
…so as mentioned earlier, you can write:
result.address2
qaswm
August 26, 2011, 12:26pm
7
7stud – wrote in post #1018604:
Here’s the relevant line:
class Address
[:firm_name, :address1, :address2, :city, :state, :zip5, :zip4].each
do |a|
attr_accessor a
…so as mentioned earlier, you can write:
result.address2
The line that was creating trouble was
.standardize.inspect
and doing result.address2 was not working.
Have removed .inspect and now it works just fine.
Thanks for everyone help.
qaswm
August 26, 2011, 12:29pm
8
…and here’s a simpler example:
module Dog
class Cat
attr_accessor :address2
def initialize
@address2 = 'hello world'
end
end
end
result = Dog::Cat.new
puts result.address2
–output:–
hello world
qaswm
August 26, 2011, 12:30pm
9
Yah. inspect() returns a String, and a String does not have an
address2() method.
qaswm
August 27, 2011, 11:22pm
10
#<USPS::Address:0x7fafdfa53b38 @zip5=“32935”, @city=“MELBOURNE”,
@address2=“1000 N WICKHAM RD”, @zip4=“8937”, @state=“FL”>
in a variable named result.
How can I access just the Address value and print it on the screen?
Just so that you know, you could also have done:
object.instance_variable_get(:@address2 )
Comes handy sometimes while inspecting an object. However, as pointed by
others, you should use the API provided by the library you are using.
qaswm
August 26, 2011, 10:01am
11
I am sorry about not asking the right question
Actually I am looking for the @address2 value.
Jean-Christophe Le Lann wrote in post #1018591:
Le 26/08/2011 09:50, Jean-Christophe Le Lann a écrit :
puts result.id
JCLL
Sorry. That is wrong, and .id is deprecated (use .object_id).
Anyway, I am not sure what you need really
class Ups
def initialize town,guy
@town ,guy=town,guy
end
end
result = Ups.new(:madrid,:diego)
puts result #Ups:0x9a46f10 <== that number ?
puts result.object_id #80885640 <== or this one ?