我正试图从红宝石中获取kinesis。我对红宝石的经验非常少。我在REPL(shell)中编写了一些代码。代码工作得很好,但由于某种原因,当我将它包装在一个函数中时,它不能按预期工作。为什么呢?
➜ ~ pry
# some definitions
[1] pry(main)> @AWS_ACCESS_KEY_ID='SOMETHING'
=> "SOMETHING"
[2] pry(main)> @AWS_SECRET_ACCESS_KEY='SECRET'
=> "SECRET"
[3] pry(main)> require 'aws-sdk'
=> true
[4] pry(main)> @limit = 100
=> 100
[5] pry(main)> Aws.config.update({region: 'us-east-1', credentials: Aws::Credentials.new(@AWS_ACCESS_KEY_ID,@AWS_SECRET_ACCESS_KEY)})
=> {:region=>"us-east-1", :credentials=>#<Aws::Credentials access_key_id="SOMETHING">}
[6] pry(main)> @kinesis = Aws::Kinesis::Client.new()
=> #<Aws::Kinesis::Client>
# bar gets a list of all streams
[7] pry(main)> def bar
[7] pry(main)* @kinesis.list_streams.stream_names
[7] pry(main)* end
# bar seems to work!
=> :bar
[8] pry(main)> bar
=> ["mystream", "my.other.stream"]
[9] pry(main)> @stream = "my.other.stream"
=> "my.other.stream"
# foo gets elements from the stream
[11] pry(main)> def foo
[11] pry(main)* puts @stream
[11] pry(main)* puts @limit
[11] pry(main)* @iter = @kinesis.get_shard_iterator(stream_name: @stream, shard_id: '0', shard_iterator_type: 'LATEST')
[11] pry(main)* @kinesis.get_records(shard_iterator: @iter.shard_iterator, limit: @limit).records.map {|x| x.data}
[11] pry(main)* end
=> :foo
# it can see vars like @limit and @stream, but it retunrs an empty list
[12] pry(main)> foo
my.other.stream
100
=> []
# this is the size of the list:
[15] pry(main)> foo.length
my.other.stream
100
=> 0
# now let's do the same as foo, but not in a function: we get size 11
[18] pry(main)> @iter2 = @kinesis.get_shard_iterator(stream_name: @stream, shard_id: '0', shard_iterator_type: 'LATEST')
=> #<struct Aws::Kinesis::Types::GetShardIteratorOutput
shard_iterator=
"AAAAAAAAAAEt2rBZY0E8eaBQgJ2sy/se4OL0VaGI6AAAAAAAAAAEt2rBZY0E8eaBQgJ2sy7A+sqE3udpLF5hQIXokaKzo8fGV4o+dpLF5hQIXokaKzo8fG+m/GJrerouDlzpr8k7CLUYT53FzOpRJdpLF5hQIXokaKzo8fGfsux80pQaGMpIpqwHw3/fvAoKyR4RhzaIQzHIBoklHf">
[19] pry(main)> (@kinesis.get_records(shard_iterator: @iter2.shard_iterator, limit: @limit).records.map {|x| x.data}).length
=> 11