Alle mercoled=C3=AC 11 luglio 2007, Max Williams ha scritto:
It seems to be a common tactic in ruby to pash what looks like a
key-value pair, in which the key is a symbol, to an action. For example
with the rails action 'render', we say
render :text =3D> "Hello world!" #(from dhh's famous blog tutorial)
Can anyone explain to me what's happening here, ie what's being passed
in? Is it the equivalent of having a (say) java method that takes a
string and saying
render("Hello world!")?
I've been trying to experiment with my own classes with this tactic, but
i'm new to ruby and can't get an example of the above working.
What is happening here is that you're calling the method render passing an=
=20
hash as argument. In ruby, when you pass an hash as the last argument to a=
=20
method, you may omit the usual braces, so the line you quoted is the same a=
s:
render( {:text =3D> "Hello world!" } )
Usually, this is used to fake keyword arguments, which ruby doesn't have.=20
As an example, imagine you want to write a method which takes a string and=
=20
does some of the following things:
* remove whitespace at the left, right or both
* makes the string downcase or upcase
* crypt it
You want the user to decide which of the previous transformation he wants t=
o=20
perform (of course, this is only an example; in reality, it might be better=
=20
to create three separate methods). The method can be written this way:
def transform_string(orig, actions =3D nil ) #1
default =3D { :remove_whitespace=3D>:both, :change_case=3D>nil, :crypt =
=3D> nil} #2
acts =3D actions || default
temp =3D orig.dup #3
if acts[:remove_whitespace] then #4
case acts[:remove_whitespace] #5
when :left then temp.lstrip! #6
when :right then temp.rstrip! #7
when :left_right then temp.strip! #8
end #9
end #10
if acts[:change_case] =3D=3D :downcase then temp.downcase! #11
elsif acts[:change_case] =3D=3D :upcase then temp.upcase! #12
end #13
temp =3D temp.crypt(acts[:crypt]) if acts[:crypt] #14
temp #15
end #16
In line 1, we define a method which takes a mandatory argument, the string=
=20
modify, and an optional argument, a hash where the user will specify which=
=20
actions he wants to perform.
In line 2, we create an hash with the default actions (the actions to perfo=
rm=20
if the user didn't pass the optional argument). This hash contains three=20
entries, two of which are nil. The only non-nil one is :remove_whitespace,=
=20
with content :both. Since actions whose value is nil won't be carried out,=
=20
this means that, if the user didn't specify an action, the method will remo=
ve=20
the whitespace both the left and the right of the string (the value=20
corresponding to :remove_whitespace in the hash is :both). In line 3, we=20
assign to the variable acts the hash containing the action specified by the=
=20
user or, if this is nil (i.e, the user didn't specify any action), the hash=
=20
with the default actions.
In lines 4-10, depending on the content of the :remove_whitespace entry of=
=20
acts, we remove the whitespace from the left, right, left and right of the=
=20
string, or we leave it unchanged.
Likewise, in lines 11-13, if the action :change_case was specified, the cas=
e=20
of the string is changed and, in line 14 we crypt the string if it was=20
requested.
NOTE
In this example, the hash with the default actions wasn't used if the user=
=20
specified the second parameter to the method. Often, you'll need to keep th=
e=20
default values for the keys the user doesn't specify. In this case, you can=
=20
do the following:
def my_method( hash =3D {} )
default_hash =3D {

pt_a =3D> value_a,

pt_b =3D> value_b }
options =3D default_hash.merge(hash)
#...
end
Here, the values to be used are obtained by merging the hash with the defau=
lt=20
options with the one passed as argument. This will overwrite the elements i=
n=20
default_hash which are also in hash, leaving the others unchanged.
I hope this helps
Stefano