Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > What exactly does "rubyish" or "ruby way" mean?

Reply
Thread Tools

What exactly does "rubyish" or "ruby way" mean?

 
 
Nit Khair
Guest
Posts: n/a
 
      10-22-2008
Over the last month I have been reading a lot of blogs, searching, going
through source code of some apps and my pickaxe. I am still unable to
clearly understand what all "the ruby way" means.

I will be perfectly happy if you can tell me:

1. phrases to google on, or check in pickaxe
2. pointers to specific libraries or classes i can study
3. link to any article I may have missed, or blog

The only thing I can put down, that i have gleaned, is the usage of
procs/blocks, i.e. allowing the user to pass in blocks/yielding a value
or self. I liked the chapter on Ruby Tk in pickaxe (since I am hoping to
write a rubyish wrapper to ncurses).

Surely there is more to the ruby way than blocks/procs/lambda. Would
really appreciate pointers.

thanks.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      10-22-2008
On Wed, Oct 22, 2008 at 8:37 AM, Nit Khair <(E-Mail Removed)> wrote:
> Over the last month I have been reading a lot of blogs, searching, going
> through source code of some apps and my pickaxe. I am still unable to
> clearly understand what all "the ruby way" means.
>
> I will be perfectly happy if you can tell me:
>
> 1. phrases to google on, or check in pickaxe
> 2. pointers to specific libraries or classes i can study
> 3. link to any article I may have missed, or blog
>
> The only thing I can put down, that i have gleaned, is the usage of
> procs/blocks, i.e. allowing the user to pass in blocks/yielding a value
> or self. I liked the chapter on Ruby Tk in pickaxe (since I am hoping to
> write a rubyish wrapper to ncurses).
>
> Surely there is more to the ruby way than blocks/procs/lambda. Would
> really appreciate pointers.


I avoid phrases like "ruby way" or words like "rubyish" simply because
the language is flexible enough to allow you to do your own thing.

With that said... my style gravitates towards conceptual, whereas some
are concerned about performance, and others about something else.

On this list, it seems people like to talk about elegance, which, to
me, means that very once in a while, someone comes up with a piece of
terse code that, at first glance, makes perfect sense; almost like a
eureka moment.

I think, very humbly, that Ruby tries its best to provide those
eureka/click moments.

So, I believe that your best answer is to read a good portion of Ruby
code (which you said you have done) and pay attention to ruby-talk.
Being rubyish is by no means a standard. Just look at Ruby Quiz to
see the variety.

Todd

 
Reply With Quote
 
 
 
 
Hugh Sasse
Guest
Posts: n/a
 
      10-22-2008


On Wed, 22 Oct 2008, Nit Khair wrote:

> Over the last month I have been reading a lot of blogs, searching, going
> through source code of some apps and my pickaxe. I am still unable to
> clearly understand what all "the ruby way" means.


Ruby is a [programming] language. To do things in the ruby way means
to write it as those who regularly (and skillfully) use the language
write it. Some say "It is possible to write FORTRAN in any language".
That is the opposite idea. Idiomatic usage, is another way to consider
it.

>
> I will be perfectly happy if you can tell me:
>
> 1. phrases to google on, or check in pickaxe
> 2. pointers to specific libraries or classes i can study
> 3. link to any article I may have missed, or blog
>
> The only thing I can put down, that i have gleaned, is the usage of
> procs/blocks, i.e. allowing the user to pass in blocks/yielding a value


Rubyists don't always do that. It depends on whether it makes sense
to do so. It is really about knowing the language and it's libraries
to be able to use them clearly and with some degree of elegance.

Well, that's my take on it, anyway.

> or self. I liked the chapter on Ruby Tk in pickaxe (since I am hoping to
> write a rubyish wrapper to ncurses).
>
> Surely there is more to the ruby way than blocks/procs/lambda. Would
> really appreciate pointers.
>
> thanks.


Hugh

 
Reply With Quote
 
Lloyd Linklater
Guest
Posts: n/a
 
      10-22-2008
Nit Khair wrote:
> Over the last month I have been reading a lot of blogs, searching, going
> through source code of some apps and my pickaxe. I am still unable to
> clearly understand what all "the ruby way" means.
>
> I will be perfectly happy if you can tell me:
>
> 1. phrases to google on, or check in pickaxe
> 2. pointers to specific libraries or classes i can study
> 3. link to any article I may have missed, or blog
>
> The only thing I can put down, that i have gleaned, is the usage of
> procs/blocks, i.e. allowing the user to pass in blocks/yielding a value
> or self. I liked the chapter on Ruby Tk in pickaxe (since I am hoping to
> write a rubyish wrapper to ncurses).
>
> Surely there is more to the ruby way than blocks/procs/lambda. Would
> really appreciate pointers.
>
> thanks.


In short, I believe that a program is "rubyish" when it is the language
solving the problem rather than the writing of an algorithm to do it, as
in Java et. al.

Here are possible examples from recent threads here. Consider how this
would be written in java, then compare it to these.

First example:

1. You are given a big (odd) string with words separated by a space:

"eggs lovely spam spam bar bar bar bar bar baz baz baz baz baz baz eggs
bar baz baz baz baz baz eggs spam spam"


2. You want to make a list of all the different words and how many
times each one occurs, then display the alphabetized list.

Just think how you would do that in an algorithm based language like
java. That is a LOT of work. Now look at a ruby way:

s = "eggs lovely spam spam bar bar bar bar bar baz baz baz baz baz baz
eggs bar baz baz baz baz baz eggs spam spam".split(/[ ]/)

p s.inject(Hash.new(0)) {|h, e| h[e] += 1; h}.sort

Second example:

You are given an array of numbers as strings: ["10.1", "7.4", "10.9",
"10.11", "10.10"]

If you do a standard sort, it turns into: ["10.1", "10.10", "10.11",
"10.9", "7.4"]

What you want is a numeric sort: ["7.4", "10.1", "10.9",
"10.10","10.11"]

Extra credit if the sort allows for the addition of numeric bits. E.g.
["10.1.5", "7.4.4", "10.9.3", "10.11.2", "10.10.3", "10.10.1"]

Ruby solution:

arr = ["10.1.5", "7.4.4", "10.9.3", "10.11.2", "10.10.3", "10.10.1"]
p arr.sort_by { |str| str.split('.').map { |x| x.to_i } }

=> ["7.4.4", "10.1.5", "10.9.3", "10.10.1", "10.10.3", "10.11.2"]
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-22-2008
2008/10/22 Nit Khair <(E-Mail Removed)>:
> Over the last month I have been reading a lot of blogs, searching, going
> through source code of some apps and my pickaxe. I am still unable to
> clearly understand what all "the ruby way" means.
>
> I will be perfectly happy if you can tell me:
>
> 1. phrases to google on, or check in pickaxe
> 2. pointers to specific libraries or classes i can study
> 3. link to any article I may have missed, or blog
>
> The only thing I can put down, that i have gleaned, is the usage of
> procs/blocks, i.e. allowing the user to pass in blocks/yielding a value
> or self. I liked the chapter on Ruby Tk in pickaxe (since I am hoping to
> write a rubyish wrapper to ncurses).
>
> Surely there is more to the ruby way than blocks/procs/lambda. Would
> really appreciate pointers.


I cannot come up with a clear cut definition, but simplicity is
certainly a part of it. Here's another example. If you want to group
multiple values and use them as Hash key you can do

MyKey = Struct.new :name, :age, :gender

or you can do

class MyKey
attr_accessor :name, :age, :gender

def eql? other
name.eql? other.name and
age.eql? other.age and
gender.eql? other.gender
end

alias == eql?

def hash
name.hash ^ age.hash ^ gender.hash
end
end

or even

class MyKey
def name; @name end
def name=(x) @name = x end
def age; @age end
def age=(x) @age = x end
def gender; @gender end
def gender=(x) @gender = x end

def initialize(name, age, gender)
self.name = name
self.age = age
self.gender = gender
end

def eql? other
name.eql? other.name and
age.eql? other.age and
gender.eql? other.gender
end

alias == eql?

def hash
name.hash ^ age.hash ^ gender.hash
end
end

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end

 
Reply With Quote
 
Nit Khair
Guest
Posts: n/a
 
      10-22-2008
Thanks a lot for all the tips.

Let's say I am writing base classes or a library to be used by others. I
am wondering that there must be things ruby provides that give a simpler
or more elegant interface to the users. Blocks was an example i gave
earlier. Yielding values in a loop was an example. I am beginning to
understand yielding self or just yield, too.

Perhaps one could give an example of Modules as in the Enumerable
module. If your class defines an "each" (iirc) then you automatically
get many methods for free.

Maybe the correct approach for me is to compare how a library is
implemented in another language (or non ruby way) versus a good ruby
implementation. e,g,
1. compare getoptlong to optparse
2. compare sqlite wrapper in java vs ruby ?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Trans
Guest
Posts: n/a
 
      10-22-2008

On Oct 22, 9:37=A0am, Nit Khair <(E-Mail Removed)> wrote:
> Over the last month I have been reading a lot of blogs, searching, going
> through source code of some apps and my pickaxe. I am still unable to
> clearly understand what all "the ruby way" means.


1. However something is done in Ruby.
2. Whatever Matz says it is.

For #1 that usually translates it into concise code. For #2 that
usually translates into concise words.

 
Reply With Quote
 
Nit Khair
Guest
Posts: n/a
 
      10-23-2008
Thomas Sawyer wrote:
> On Oct 22, 9:37´┐Żam, Nit Khair <(E-Mail Removed)> wrote:
>> Over the last month I have been reading a lot of blogs, searching, going
>> through source code of some apps and my pickaxe. I am still unable to
>> clearly understand what all "the ruby way" means.

>
> 1. However something is done in Ruby.
> 2. Whatever Matz says it is.
>
> For #1 that usually translates it into concise code. For #2 that
> usually translates into concise words.


So my code and the user of the library should have the least code to
write, and of course it has to be simple and clear, while at the same
time the simplicity should not rob the user of power/customizability
etc.
Shall keep the above in mind. (sorry for not being as concise as you
)

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-23-2008
2008/10/23 Nit Khair <(E-Mail Removed)>:

> So my code and the user of the library should have the least code to
> write, and of course it has to be simple and clear, while at the same
> time the simplicity should not rob the user of power/customizability
> etc.
> Shall keep the above in mind. (sorry for not being as concise as you
> )


I'd add that simplicity for the user of the lib is more important than
simplicity for the implementer of the lib.

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      10-23-2008
On Thu, Oct 23, 2008 at 9:20 AM, Robert Klemme
<(E-Mail Removed)> wrote:
> 2008/10/23 Nit Khair <(E-Mail Removed)>:
>
>> So my code and the user of the library should have the least code to
>> write, and of course it has to be simple and clear, while at the same
>> time the simplicity should not rob the user of power/customizability
>> etc.
>> Shall keep the above in mind. (sorry for not being as concise as you
>> )

>
> I'd add that simplicity for the user of the lib is more important than
> simplicity for the implementer of the lib.

And it might be one of the rare occasion where performance counts.

As an example I once submitted an *ugly* patch using each_with_index
instead of inject to Factes which made a
method which was just elegant ugly, but it gained 10% of performance.
The maintainer judged that my code was still readable enough to accept
my patch because the method might be heavily used.No one would ever
consider changing
this method in an application unless performance was not good enough.

This just to underline how important Robert's point is.
Robert
--=20
C'est v=E9ritablement utile puisque c'est joli.

Antoine de Saint Exup=E9ry

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How exactly does STP traverse the network? Josh T Cisco 3 12-08-2005 04:06 AM
What exactly does dropdownlist.clear do... Ryan ASP .Net 2 11-08-2005 08:50 PM
What does ViewStateUserKey do exactly? Henri ASP .Net 3 11-29-2004 07:05 PM
How *exactly* does Smart Navigation work? anon ASP .Net 6 01-06-2004 08:18 PM
Multicast? what exactly it does? How useful it is? Bay MCSE 2 10-25-2003 06:21 PM



Advertisments