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

Posted by Panagiotis Atmatzidis (Guest)
on 2013-01-20 15:08
(Received via mailing list)
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
Posted by "Sandor Szücs" <sandor.szuecs@fu-berlin.de> (Guest)
on 2013-01-20 15:27
(Received via mailing list)
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
Posted by Panagiotis Atmatzidis (Guest)
on 2013-01-20 17:48
(Received via mailing list)
Thanks,

that was an awesome explanation! :-)

On 20 Ιαν 2013, at 15:26 , Sandor Szücs <sandor.szuecs@fu-berlin.de> 
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:  atma@convalesco.org
URL:  http://www.convalesco.org
GnuPG ID: 0xE736C6A0
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xE736C6A0
Posted by Robert Klemme (robert_k78)
on 2013-01-20 18:55
(Received via mailing list)
On Sun, Jan 20, 2013 at 3:26 PM, Sandor Szcs
<sandor.szuecs@fu-berlin.de> 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
Posted by Josh Cheek (josh-cheek)
on 2013-01-21 23:07
(Received via mailing list)
On Sun, Jan 20, 2013 at 8:08 AM, Panagiotis Atmatzidis
<atma@convalesco.org>wrote:

> 2) 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"]
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.