![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
This probably does the same thing:
class Array
def max_by(&block)
max { |a, b| block.call(a) <=> block.call(b) }
end
end |
|
|
|
|
|
#12 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#13 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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 ![]() |
|
|
|
|
|
#14 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#15 |
|
Programmer
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3
![]() |
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 ^_^ |
|
|
|
|
|
#16 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
I think Dietrich was just pointing out that, unlike Python, Ruby doesn't support named arguments to methods.
|
|
|
|
|
|
#17 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
|
#18 |
|
Programmer
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3
![]() |
never mind i know what it mean now
http://diveintopython.org/power_of_i...arguments.html |
|
|
|
|
|
#19 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
Quote:
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. EDIT lease 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! |
|
|
|
|
|
|
#20 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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
__________________
I looked it up on the Intergnats! Last edited by Dietrich; Apr 14th, 2006 at 10:07 AM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|