Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 14th, 2006, 11:21 AM   #21
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
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 } #=> 55

factorial:

(1..6).inject(1) { |product, elem| product * elem } #=> 720

fill 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!
Jessehk is offline   Reply With Quote
Old Apr 14th, 2006, 11:37 AM   #22
Master
Programmer
 
Master's Avatar
 
Join Date: Sep 2005
Location: GA
Posts: 99
Rep Power: 4 Master is on a distinguished road
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
Master is offline   Reply With Quote
Old Apr 15th, 2006, 12:02 AM   #23
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Apr 15th, 2006, 5:07 AM   #24
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
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!
Dietrich is offline   Reply With Quote
Old Apr 15th, 2006, 6:23 AM   #25
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Talking

Keep finding these strange things:
puts c = !c  # true
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Apr 15th, 2006, 9:48 AM   #26
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
Quote:
Originally Posted by Dietrich
Jessehk,

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.
The reason is that default arguments are a bit of a hack. Basically, you're telling the method to accept a hash. That hash could consist of any keys and values the user chooses.

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!
Jessehk is offline   Reply With Quote
Old Jun 22nd, 2006, 11:49 AM   #27
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Jul 8th, 2006, 8:40 PM   #28
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
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
end

Example 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(&:*) #product

Full explanation: http://blogs.pragprog.com/cgi-bin/pr...by/ToProc.rdoc
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jul 9th, 2006, 11:02 AM   #29
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

Jessehk, great hints of nice code! Also the URLs, Thanks
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jul 9th, 2006, 2:51 PM   #30
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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!
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 3:58 PM.

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