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: atma@convalesco.org
URL: http://www.convalesco.org
GnuPG ID: 0xE736C6A0
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xE736C6A0

···

--
The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."

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]

2) How should be defined the "feed_list" method in order to be called like: feed = NewsDisplay::feed_list

  1 class NewsDisplay¬
  2 def self.feed_list(*args)¬
  3 new(*args).feed_list¬
  4 end¬
  5 def initialize(*args)¬
  6 end¬
  7 def feed_list¬
  8 ['f','e','e','d']¬
  9 end¬
10 end¬
11 def test¬
12 feed = NewsDisplay::feed_list¬
13 end¬

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

Hth.

···

On 1/20/13 3:08 PM, Panagiotis Atmatzidis wrote:
--
All the best, Sandor Szücs

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 8:08 AM, Panagiotis Atmatzidis <atma@convalesco.org>wrote:

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: atma@convalesco.org
URL: http://www.convalesco.org
GnuPG ID: 0xE736C6A0
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xE736C6A0
--
The wise man said: "Never argue with an idiot. They bring you down to
their level and beat you with experience."

Thanks,

that was an awesome explanation! :slight_smile:

···

On 20 Ιαν 2013, at 15:26 , Sandor Szücs <sandor.szuecs@fu-berlin.de> wrote:

On 1/20/13 3:08 PM, Panagiotis Atmatzidis 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]

2) How should be defined the "feed_list" method in order to be called like: feed = NewsDisplay::feed_list

1 class NewsDisplay¬
2 def self.feed_list(*args)¬
3 new(*args).feed_list¬
4 end¬
5 def initialize(*args)¬
6 end¬
7 def feed_list¬
8 ['f','e','e','d']¬
9 end¬
10 end¬
11 def test¬
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: atma@convalesco.org
URL: http://www.convalesco.org
GnuPG ID: 0xE736C6A0
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xE736C6A0
--
The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."

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

···

On Sun, Jan 20, 2013 at 3:26 PM, Sandor Szücs <sandor.szuecs@fu-berlin.de> wrote:

On 1/20/13 3:08 PM, Panagiotis Atmatzidis 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]

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/