Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jul 9th, 2006, 3:45 PM   #31
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 Dietrich
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
puts states50.index("Iowa") #=> 14

__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jul 9th, 2006, 9:07 PM   #32
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

puts states50.index("Iowa") #=> 14
Thanks, I thought there should be a find-index function builtin into Ruby.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jul 9th, 2006, 9:13 PM   #33
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
class Symbol
    def to_proc
        lambda { |obj, *args| obj.send(self, *args) }
    end
end
Is this what some folks refer to as metaprogramming?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jul 9th, 2006, 11:07 PM   #34
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:
Is this what some folks refer to as metaprogramming?
Not quite. I actually don't know about metaprogamming in Ruby, though this tutorial might help: http://www.devsource.com/article2/0,1895,1928561,00.asp
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jul 27th, 2006, 8:50 AM   #35
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
I found a nice collection of Ruby code snippets here:
http://textsnippets.com/tag/ruby
__________________
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 5:18 AM.

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