2Q: Getting first 4 elements from array and calling class methods directly

Hello fellow Rubyists!

I have written a quick n dirty code to display the first 4 RSS entries
from a feed using “feedzirra” gem. The code is pretty straight forward:
https://gist.github.com/4578863

I have two questions, related to this code:

  1. Is there any more elegant[1] way to get the first for elements of
    @entries’ object array?
  2. How should be defined the “feed_list” method in order to be called
    like: feed = NewsDisplay::feed_list

I know I should put a ‘self’ reference somewhere. A little explanation
of who this technique works would be nice. I know that Class::method
calls might be
deprecated in the future. However even “Class.method” looks much more
elegant and I’d like to know how to implement it.

Best Regards,

[1] Elegant = Easy to read, concise code snippet. Cryptic snippets are
not elegant imho.

Panagiotis (atmosx) Atmatzidis

email: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0xE736C6A0
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xE736C6A0

On 1/20/13 3:08 PM, Panagiotis A. wrote:

  1. Is there any more elegant[1] way to get the first for elements of ‘@entries
    object array?

irb:0> a = (1…10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb:0> a.take(3)
=> [1, 2, 3]
irb:0> a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb:0> a.pop(3)
=> [8, 9, 10]
irb:0> a
=> [1, 2, 3, 4, 5, 6, 7]
irb:0> a.shift(3)
=> [1, 2, 3]
irb:0> a
=> [4, 5, 6, 7]

  1. How should be defined the “feed_list” method in order to be called like: feed
    = NewsDisplay::feed_list

1 class NewsDisplay

Thanks,

that was an awesome explanation! :slight_smile:

On 20 Ιαν 2013, at 15:26 , Sandor Szücs [email protected]
wrote:

irb:0> a.pop(3)
1 class NewsDisplay¬
12 feed = NewsDisplay::feed_list¬
13 end¬

irb:0> test
=> [“f”, “e”, “e”, “d”]

Hth.

All the best, Sandor Szücs

Panagiotis (atmosx) Atmatzidis

email: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0xE736C6A0
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xE736C6A0

On Sun, Jan 20, 2013 at 8:08 AM, Panagiotis A.
[email protected]wrote:

  1. How should be defined the “feed_list” method in order to be called
    [1] Elegant = Easy to read, concise code snippet. Cryptic snippets are not
    their level and beat you with experience."

I prefer bracket notation because it works anywhere in an array

a = [*‘a’…‘e’] # => [“a”, “b”, “c”, “d”, “e”]

start_index, length

a[0, 3] # => [“a”, “b”, “c”]

start_index…end_index

a[0…3] # => [“a”, “b”, “c”, “d”]

On Sun, Jan 20, 2013 at 3:26 PM, Sandor Szcs
[email protected] wrote:

irb:0> a.pop(3)
=> [8, 9, 10]
irb:0> a
=> [1, 2, 3, 4, 5, 6, 7]
irb:0> a.shift(3)
=> [1, 2, 3]
irb:0> a
=> [4, 5, 6, 7]

And then there is

irb(main):001:0> a = (1…10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):002:0> a.first(3)
=> [1, 2, 3]

Cheers

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs