Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 10th, 2006, 3:37 AM   #11
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
This probably does the same thing:
class Array
    def max_by(&block)
        max { |a, b| block.call(a) <=> block.call(b) }
    end
end
Arevos is offline   Reply With Quote
Old Apr 10th, 2006, 3:48 PM   #12
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
I'll best you one day, Arevos!




(In actuality, this was a case of me still discovering the available Enumerable methods . )
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Apr 10th, 2006, 5:20 PM   #13
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
A good way to approach these things is to assume that common functions exist and go from there. Ruby must have had some manner of max/min functions, so I just pulled up a Ruby language reference and searched for "max".

Educated guesses and dumb luck can turn up surprisingly efficient results, sometimes
Arevos is offline   Reply With Quote
Old Apr 11th, 2006, 10:18 PM   #14
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Talking

Found this cute little snippet:
[PHP]# just a teaser
print "Are you stupid? (yes or no) "
a = gets.to_s
if a == "yes"
puts "this is true"
else
puts "this is false"
end
gets # console wait[/PHP]
Also, Pythonistas beware!
[PHP]# function arg. defaults work in Python, but not in Ruby

def funk(a=2, b=3)
print "a=#{a} b=#{b}\n"
end

funk() # a=2 b=3

funk(a=1) # a=1 b=3

funk(b=1) # a=1 b=3 oops!

funk(a=12, b=13) # a=12 b=13[/PHP]
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Apr 12th, 2006, 8:33 AM   #15
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3 Master is on a distinguished road
defualt values works in ruby

def doSomething(x = 10, y=4)
sprintf("%s %s",x,y)
end
doSomething() returns 10 and 4
doSomething(5) return 5 and 4
doSomething(6,7) return 6 and 7

when calling a function that has default values such as mine
you don't need to call doSomething(x = 5, y = 9) you simply do
doSomething(5,9)
x and y are just parameter which exist within the function not globally.
Your example use
funk(b=1) does not make it to call the b part of your function
but that code simply set b to 1 then pass b which is also = to 1 to the first parameter which is a locally to the function called funk then it changes a in the function to 1 then return the result. I think you are mixing default with something else or maybe you don't understand what is mean by default. here is a sample in C++

void doSomething(int x = 8, int y = 9)
{
std::cout << x << ","<<y;
}
when you call that you don't have to create a variable with the same name of the parameter, creating one then setting it to the function will not make no different
if i make int y outside of the function and set it to 70, then pass it to myfunction. it is interpreted as you passing 70 to the first parameter, not the second. the purpose of defualt value is simple makes it that you don't have to call every argument for the function call. it is just like having a defualt constructor in your program

sorry if i wrote too much or misunderstand what you said ^_^
Master is offline   Reply With Quote
Old Apr 12th, 2006, 8:57 AM   #16
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
I think Dietrich was just pointing out that, unlike Python, Ruby doesn't support named arguments to methods.
Arevos is offline   Reply With Quote
Old Apr 12th, 2006, 9:17 AM   #17
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Quote:
Originally Posted by Arevos
I think Dietrich was just pointing out that, unlike Python, Ruby doesn't support named arguments to methods.
Thanks Arevos, you have to know Python to fall for this potential mistake. Named arguments seem to be a nice feature that is missing in Ruby.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Apr 12th, 2006, 3:53 PM   #18
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3 Master is on a distinguished road
never mind i know what it mean now
http://diveintopython.org/power_of_i...arguments.html
Master is offline   Reply With Quote
Old Apr 12th, 2006, 4:02 PM   #19
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by Master
class Person:
    def __init__(self, name, age):
        self.name, self.age = name, age
    
p = Person(name="Jesse", age=15)

#or

p = Person(age=26, name="Joe")

I will admit that Ruby does not have named arguments, but it is planned for the 2.0 release.

EDITlease don't edit out a question, Master. It is really annoying for those answering it.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Apr 14th, 2006, 9:53 AM   #20
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Unhappy

Any wxPython (GUI) code in general uses a lot of named arguments. If Ruby does not have named arguments, then that explains why wxRuby code looks sooo different!

I looked at a hash as a workaround, but that has real problems too! It doesn't use the given default values.

# using a dictionary/hash container, still problems
def funk2(hash1 = {:a => 2, :b => 3}) 
  print "a=#{hash1[:a]}  b=#{hash1[:b]}\n" 
end 

funk2()     # a=2  b=3
funk2(:a=>1)  # a=1  b=  b should be 3 
funk2(:b=>1)  # a=  b=1  a should be 2 
funk2(:a=>12, :b=>13)  # a=12  b=13
Does anybody know how to do this right in Ruby, before the code looks alltogether too homely?
__________________
I looked it up on the Intergnats!

Last edited by Dietrich; Apr 14th, 2006 at 10:07 AM.
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC