What is This Construct?

Discussion in 'Ruby' started by Travis Tslib, Oct 19, 2010.

  1. Travis Tslib

    Travis Tslib Guest

    I'm messing with a gem called eve-api. I have a Perl and PHP
    background. I'm messing with Ruby for the first time, and I've not read
    any manual cover to cover. I'm crashing into this.

    I've looked in Ruby Essentials and a few other places returned from a
    Google query. In the gem is a file defining:




    def account_transactions(character_id, type, account_key)
    transactions = []
    doc = @transport.call("#{type.to_s}/WalletTransactions.xml.aspx",
    :characterID => character_id, :accountKey => account_key)
    doc.each_element('//rowset[@name="transactions"]/row') do |row|
    transaction = {
    :date => DateTime.parse(row.attributes['transactionDateTime']),
    :item => {
    :id => row.attributes['typeID'].to_i,
    :name => row.attributes['typeName']
    }
    }
    transactions << transaction
    end
    return transactions
    end



    I've truncated that a bit.

    In my code, when I call this, I can

    myAccount.transactions.each do |trx|
    puts "#{trx.date}"
    end


    I can't get to the ID or Name of the item. I've tried trx.item.id and
    trx.item[0].id and a few other things. What is that? How can I access
    it? The thing that really confuses me is trx.date works.

    --
    Posted via http://www.ruby-forum.com/.
     
    Travis Tslib, Oct 19, 2010
    #1
    1. Advertising

  2. > I can't get to the ID or Name of the item. =A0I've tried trx.item.id and
    > trx.item[0].id and a few other things. =A0What is that? =A0How can I acce=

    ss
    > it? =A0The thing that really confuses me is trx.date works.



    It's a hash. Try this:

    myAccount.transactions.each do |trx|
    puts "#{trx[:item][:id]}: #{trx[:item][:name} (on #{trx[:item][:date]})"
    end
     
    Steve Klabnik, Oct 19, 2010
    #2
    1. Advertising

  3. Travis Tslib

    Ryan Davis Guest

    On Oct 18, 2010, at 18:40 , Travis Tslib wrote:

    > I can't get to the ID or Name of the item. I've tried trx.item.id and
    > trx.item[0].id and a few other things. What is that? How can I =

    access
    > it? The thing that really confuses me is trx.date works.


    transaction =3D {
    :date =3D> DateTime.parse(row.attributes['transactionDateTime']),
    :item =3D> {
    :id =3D> row.attributes['typeID'].to_i,
    :name =3D> row.attributes['typeName']
    }
    }

    that's a nested hash, just like you'd use in perl.

    > myAccount.transactions.each do |trx|
    > puts "#{trx.date}"
    > end


    that shouldn't work... are you loading something magical? Eve-api might =
    be doing it. I've never used that... Ah. Looking at the code I see:

    class Base # omg I hate that name for class hierarchies. DHH be =
    damned.
    def method_missing(method, *args)
    return(@options[method]) if @options.has_key?(method)
    raise(Exceptions::AttributeException,"unknown attribute =
    #{method}")
    end
    end

    class Transaction < Base; end

    So, yeah. that's why date works... and item. But since it probably isn't =
    recursively creating the objects, the sub-sub items won't work the same =
    way. You'll have to use hash syntax for those (I'd file a bug too):

    trx.item[:id] and trx.item[:name]

    should both work fine.
     
    Ryan Davis, Oct 19, 2010
    #3
  4. Travis Tslib

    Travis Tslib Guest

    Thanks. After Steve's comment, I tried the exact thing that Ryan
    recommends. But thanks for the explanation, Ryan. I'm new to Ruby and
    I couldn't figure out why it worked one way and not another.

    It is all working now.

    --
    Posted via http://www.ruby-forum.com/.
     
    Travis Tslib, Oct 19, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Floresita
    Replies:
    3
    Views:
    5,512
    Oggie
    Apr 2, 2004
  2. Kuan Zhou

    Construct synthesis problem

    Kuan Zhou, Apr 22, 2005, in forum: VHDL
    Replies:
    2
    Views:
    524
    Mohammed A khader
    Apr 22, 2005
  3. Alif Wahid

    The 'impure' construct

    Alif Wahid, Jan 4, 2006, in forum: VHDL
    Replies:
    2
    Views:
    8,322
    Reiner Huober
    Jan 4, 2006
  4. Replies:
    2
    Views:
    4,759
    Thomas Stanka
    Mar 27, 2006
  5. Mukesh
    Replies:
    4
    Views:
    626
    Paul N
    Mar 26, 2010
Loading...

Share This Page