![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
I just thought I'd include a few uses of Inumerable#inject.
http://ruby-doc.org/core/ It's pretty neat. sum: (1..10).inject { |sum, elem| sum + elem } #=> 55factorial: (1..6).inject(1) { |product, elem| product * elem } #=> 720fill an array (like the functional map): (1..10).inject([]) { |result, elem| result << elem ** 2 }
#=> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]searching for a value: %w(This is a wonderful test).inject { |big, word| word.length > big.length? word : big }
#=> "wonderful"
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#22 |
|
Programmer
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 3
![]() |
I guess you still don't understand ruby quite well. When you defualt parameter is an hash. then you pass just :a=>1, that mean you are replacing the defualt hash with the one which you pass that contains only a, by passing hash as the defualt parameter, your argument can be over millions if you want, all it will basically do is put all those million of values into an hash and print just the one you said it should print. you passing :a=>1 mean nothing to the hash defualt, i can even use "45"=> "myName" and it will accept it. you can print out all the hash
by using myhash.values or myhash.keys |
|
|
|
|
|
#23 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
Master is right.
The correct way to do it is like this:
def func(args = {:a => 2, :b => 3}) #default values
puts "a: #{a}\nb: #{b}"
end
func(:a => 3, :b => 4)
#=> a: 3
# b: 4
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#24 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Jessehk,
thanks for your inject examples, they are pretty neat! On the default argument discussion, I get stuck with the fact that you only get the correct defaults, if you don't use any arguments at all.
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#25 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Keep finding these strange things:
puts c = !c # true
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#26 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
Quote:
Like I said though, named arguments are planned (or so I hear) for the 2.0 release.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#27 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
I was just searching through the API documentation at http://ruby-doc.org and I found a really cool trick with Integer#to_s
If you use it normally, like so: >> 3.to_s => "3" >> 100.to_s => "100" you get results as expected. What I didn't realize is that if supply an Integer from 1 to 36, it will convert the number to that base. >> 33.to_s(2) #base 2, or binary => "100001" >> 33.to_s(8) #base 8, or octal => "41" >> 31231313113123.to_s(16) #base 16, or hexadecimal => "1c679b52f023" >> 31313231312.to_s(36) #base 36, the largest supported => "edv37ww" I thought it was neat. ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#28 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
This trick is just cool.
Imagine being able to do things like this: ["Joe", "Sally", "Phil"].map(&:upcase) #=> ["JOE", "SALLY", "PHIL"] With this snippet, you can. I did not think of this.
class Symbol
def to_proc
lambda { |obj, *args| obj.send(self, *args) }
end
endExample usage: require 'jesse_symbol' #where new code is stored
class Integer
def square
self * self
end
end
p (1..10).map(&:square)
#or even
p (1..10).inject(&:+) #sum
p (1..10).inject(&:*) #productFull explanation: http://blogs.pragprog.com/cgi-bin/pr...by/ToProc.rdoc
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#29 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Jessehk, great hints of nice code! Also the URLs, Thanks
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#30 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
I found this simple example:
# an array of the 50 states
states50 = [
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia',
'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas',
'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts',
'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana',
'Nebraska', 'Nevada', 'New_Hampshire', 'New_Jersey', 'New_Mexico',
'New_York', 'North_Carolina', 'North_Dakota', 'Ohio', 'Oklahoma',
'Oregon', 'Pennsylvania', 'Rhode_Island', 'South_Carolina',
'South_Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West_Virginia', 'Wisconsin', 'Wyoming'
]
puts states50.length() # () is optional --> 50
puts states50 # displays 50 states, one per line
def find(arr, value)
for i in 0...arr.length
return i if value == arr[i]
end
end
puts find(states50, "Iowa") # found at index --> 14
__________________
I looked it up on the Intergnats! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|